Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
postgres-operator
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
GitHub Mirror
zalando
postgres-operator
Commits
7abba862
Commit
7abba862
authored
Jun 9, 2017
by
Murat Kabilov
Browse files
Options
Downloads
Patches
Plain Diff
move exec method from separate file to the utils;
move regexps to the utils
parent
e0dacd0c
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
pkg/cluster/cluster.go
+0
-6
0 additions, 6 deletions
pkg/cluster/cluster.go
pkg/cluster/exec.go
+0
-61
0 additions, 61 deletions
pkg/cluster/exec.go
pkg/cluster/util.go
+59
-0
59 additions, 0 deletions
pkg/cluster/util.go
with
59 additions
and
67 deletions
pkg/cluster/cluster.go
+
0
−
6
View file @
7abba862
...
...
@@ -7,7 +7,6 @@ import (
"encoding/json"
"fmt"
"reflect"
"regexp"
"sync"
"github.com/Sirupsen/logrus"
...
...
@@ -29,11 +28,6 @@ import (
"github.com/zalando-incubator/postgres-operator/pkg/util/volumes"
)
var
(
alphaNumericRegexp
=
regexp
.
MustCompile
(
"^[a-zA-Z][a-zA-Z0-9]*$"
)
userRegexp
=
regexp
.
MustCompile
(
`^[a-z0-9]([-_a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-_a-z0-9]*[a-z0-9])?)*$`
)
)
// Config contains operator-wide clients and configuration used from a cluster. TODO: remove struct duplication.
type
Config
struct
{
KubeClient
*
kubernetes
.
Clientset
//TODO: move clients to the better place?
...
...
This diff is collapsed.
Click to expand it.
pkg/cluster/exec.go
deleted
100644 → 0
+
0
−
61
View file @
e0dacd0c
package
cluster
import
(
"bytes"
"fmt"
remotecommandconsts
"k8s.io/apimachinery/pkg/util/remotecommand"
"k8s.io/client-go/pkg/api"
"k8s.io/kubernetes/pkg/client/unversioned/remotecommand"
"github.com/zalando-incubator/postgres-operator/pkg/spec"
)
func
(
c
*
Cluster
)
ExecCommand
(
podName
*
spec
.
NamespacedName
,
command
...
string
)
(
string
,
error
)
{
var
(
execOut
bytes
.
Buffer
execErr
bytes
.
Buffer
)
pod
,
err
:=
c
.
KubeClient
.
Pods
(
podName
.
Namespace
)
.
Get
(
podName
.
Name
)
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"could not get pod info: %v"
,
err
)
}
if
len
(
pod
.
Spec
.
Containers
)
!=
1
{
return
""
,
fmt
.
Errorf
(
"could not determine which container to use"
)
}
req
:=
c
.
RestClient
.
Post
()
.
Resource
(
"pods"
)
.
Name
(
podName
.
Name
)
.
Namespace
(
podName
.
Namespace
)
.
SubResource
(
"exec"
)
req
.
VersionedParams
(
&
api
.
PodExecOptions
{
Container
:
pod
.
Spec
.
Containers
[
0
]
.
Name
,
Command
:
command
,
Stdout
:
true
,
Stderr
:
true
,
},
api
.
ParameterCodec
)
exec
,
err
:=
remotecommand
.
NewExecutor
(
c
.
RestConfig
,
"POST"
,
req
.
URL
())
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"failed to init executor: %v"
,
err
)
}
err
=
exec
.
Stream
(
remotecommand
.
StreamOptions
{
SupportedProtocols
:
remotecommandconsts
.
SupportedStreamingProtocols
,
Stdout
:
&
execOut
,
Stderr
:
&
execErr
,
})
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"could not execute: %v"
,
err
)
}
if
execErr
.
Len
()
>
0
{
return
""
,
fmt
.
Errorf
(
"stderr: %v"
,
execErr
.
String
())
}
return
execOut
.
String
(),
nil
}
This diff is collapsed.
Click to expand it.
pkg/cluster/util.go
+
59
−
0
View file @
7abba862
package
cluster
import
(
"bytes"
"encoding/json"
"fmt"
"strings"
"regexp"
"time"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/apis/apps/v1beta1"
"k8s.io/client-go/pkg/labels"
remotecommandconsts
"k8s.io/apimachinery/pkg/util/remotecommand"
"k8s.io/client-go/pkg/api"
"k8s.io/kubernetes/pkg/client/unversioned/remotecommand"
"github.com/zalando-incubator/postgres-operator/pkg/spec"
"github.com/zalando-incubator/postgres-operator/pkg/util"
...
...
@@ -16,6 +21,11 @@ import (
"github.com/zalando-incubator/postgres-operator/pkg/util/retryutil"
)
var
(
alphaNumericRegexp
=
regexp
.
MustCompile
(
"^[a-zA-Z][a-zA-Z0-9]*$"
)
userRegexp
=
regexp
.
MustCompile
(
`^[a-z0-9]([-_a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-_a-z0-9]*[a-z0-9])?)*$`
)
)
func
isValidUsername
(
username
string
)
bool
{
return
userRegexp
.
MatchString
(
username
)
}
...
...
@@ -302,3 +312,52 @@ func (c *Cluster) credentialSecretName(username string) string {
func
(
c
*
Cluster
)
podSpiloRole
(
pod
*
v1
.
Pod
)
string
{
return
pod
.
Labels
[
c
.
OpConfig
.
PodRoleLabel
]
}
func
(
c
*
Cluster
)
ExecCommand
(
podName
*
spec
.
NamespacedName
,
command
...
string
)
(
string
,
error
)
{
var
(
execOut
bytes
.
Buffer
execErr
bytes
.
Buffer
)
pod
,
err
:=
c
.
KubeClient
.
Pods
(
podName
.
Namespace
)
.
Get
(
podName
.
Name
)
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"could not get pod info: %v"
,
err
)
}
if
len
(
pod
.
Spec
.
Containers
)
!=
1
{
return
""
,
fmt
.
Errorf
(
"could not determine which container to use"
)
}
req
:=
c
.
RestClient
.
Post
()
.
Resource
(
"pods"
)
.
Name
(
podName
.
Name
)
.
Namespace
(
podName
.
Namespace
)
.
SubResource
(
"exec"
)
req
.
VersionedParams
(
&
api
.
PodExecOptions
{
Container
:
pod
.
Spec
.
Containers
[
0
]
.
Name
,
Command
:
command
,
Stdout
:
true
,
Stderr
:
true
,
},
api
.
ParameterCodec
)
exec
,
err
:=
remotecommand
.
NewExecutor
(
c
.
RestConfig
,
"POST"
,
req
.
URL
())
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"failed to init executor: %v"
,
err
)
}
err
=
exec
.
Stream
(
remotecommand
.
StreamOptions
{
SupportedProtocols
:
remotecommandconsts
.
SupportedStreamingProtocols
,
Stdout
:
&
execOut
,
Stderr
:
&
execErr
,
})
if
err
!=
nil
{
return
""
,
fmt
.
Errorf
(
"could not execute: %v"
,
err
)
}
if
execErr
.
Len
()
>
0
{
return
""
,
fmt
.
Errorf
(
"stderr: %v"
,
execErr
.
String
())
}
return
execOut
.
String
(),
nil
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment