-
Notifications
You must be signed in to change notification settings - Fork 786
/
step_replicate.go
194 lines (168 loc) · 6.34 KB
/
step_replicate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package step
import (
"fmt"
"regexp"
"strings"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/pkg/errors"
"github.com/rollout/rox-go/core/utils"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ReplicateOptions contains the command line flags
type ReplicateOptions struct {
step.StepOptions
ReplicateToNamepace []string
CreateNamespace bool
}
const (
annotationReplicationAllowed = "replicator.v1.mittwald.de/replication-allowed"
annotationEeplicationAllowedNamespaces = "replicator.v1.mittwald.de/replication-allowed-namespaces"
configMap = "configmap"
secret = "secret" // pragma: allowlist secret
replicateToNamespaceFlag = "replicate-to-namespace"
// Description common text for long command descriptions around storage
Description = `
Annotates a secret or configmap so it can be replicated across an environment
`
)
var (
stepReplicateLong = templates.LongDesc(`
Works with the replicator app https://github.com/jenkins-x-charts/kubernetes-replicator
jx add app replicator
This step will annotate a secret or configmap so that the replicator can replicate the data into another namespace
`)
stepReplicateExample = templates.Examples(`
NOTE: quote and namespaces that include a wildcard
# lets collect some files to the team's default storage location (which if not configured uses the current git repository's gh-pages branch)
jx step replicate configmap foo --replicate-to-namespace jx-staging --replicate-to-namespace "foo-preview*"
# lets collect some files to the team's default storage location (which if not configured uses the current git repository's gh-pages branch)
jx step replicate secret bar --replicate-to-namespace jx-staging --replicate-to-namespace "foo-preview*"
`)
)
// NewCmdStepReplicate creates the CLI command
func NewCmdStepReplicate(commonOpts *opts.CommonOptions) *cobra.Command {
options := ReplicateOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "replicate",
Short: Description,
Long: stepReplicateLong,
Example: stepReplicateExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringArrayVarP(&options.ReplicateToNamepace, replicateToNamespaceFlag, "r", nil, "Specify a list of namespaces to replicate data into")
cmd.Flags().BoolVarP(&options.CreateNamespace, "create-namespace", "", false, "Should create any missing namespaces")
return cmd
}
// Run runs the command
func (o *ReplicateOptions) Run() error {
if len(o.Args) != 2 {
return util.MissingArgument("configmap or secret")
}
if o.Args[0] != secret && o.Args[0] != configMap {
return util.MissingArgument("configmap or secret")
}
if len(o.ReplicateToNamepace) == 0 {
return util.MissingOption(replicateToNamespaceFlag)
}
client, currentNamespace, err := o.KubeClientAndNamespace()
if err != nil {
return err
}
for _, ns := range o.ReplicateToNamepace {
// if there's a wildcard in the name let's not validate it exists
if strings.Contains(ns, "*") {
continue
}
_, err := client.CoreV1().Namespaces().Get(ns, metav1.GetOptions{})
if err != nil {
if o.CreateNamespace {
err = kube.EnsureNamespaceCreated(client, ns, nil, nil)
if err != nil {
return err
}
} else {
return util.InvalidOptionError(replicateToNamespaceFlag, ns, err)
}
}
}
resourceType := o.Args[0]
resourceName := o.Args[1]
if resourceType == secret {
// find all secrets issued by letsencrypt
if strings.Contains(resourceName, "*") {
secrets, err := client.CoreV1().Secrets(currentNamespace).List(metav1.ListOptions{})
if err != nil {
return errors.Wrap(err, fmt.Sprintf("unable to list secrets '%s'", resourceName))
}
re := regexp.MustCompile(resourceName)
for _, secretItem := range secrets.Items {
if re.MatchString(secretItem.Name) {
secret, err := client.CoreV1().Secrets(currentNamespace).Get(secretItem.Name, metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, fmt.Sprintf("unable to get secret '%s'", secretItem.Name))
}
secret.Annotations = setReplicatorAnnotations(secret.Annotations, o.ReplicateToNamepace)
_, err = client.CoreV1().Secrets(currentNamespace).Update(secret)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("unable to update secret '%s'", secretItem.Name))
}
}
}
} else {
secret, err := client.CoreV1().Secrets(currentNamespace).Get(resourceName, metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, fmt.Sprintf("unable to get secret '%s'", resourceName))
}
secret.Annotations = setReplicatorAnnotations(secret.Annotations, o.ReplicateToNamepace)
_, err = client.CoreV1().Secrets(currentNamespace).Update(secret)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("unable to update secret '%s'", resourceName))
}
}
}
if resourceType == configMap {
cm, err := client.CoreV1().ConfigMaps(currentNamespace).Get(resourceName, metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, fmt.Sprintf("unable to get configmap %s", resourceName))
}
cm.Annotations = setReplicatorAnnotations(cm.Annotations, o.ReplicateToNamepace)
_, err = client.CoreV1().ConfigMaps(currentNamespace).Update(cm)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("unable to update configmap %s", resourceName))
}
}
return nil
}
func setReplicatorAnnotations(annotations map[string]string, namespaces []string) map[string]string {
if annotations == nil {
annotations = make(map[string]string)
}
annotations[annotationReplicationAllowed] = "true"
allowedNamespaces := annotations[annotationEeplicationAllowedNamespaces]
var nsSlice []string
if allowedNamespaces != "" {
nsSlice = strings.Split(allowedNamespaces, ",")
}
for _, replicateToNamespace := range namespaces {
if !utils.ContainsString(nsSlice, replicateToNamespace) {
nsSlice = append(nsSlice, replicateToNamespace)
}
}
annotations[annotationEeplicationAllowedNamespaces] = strings.Join(nsSlice, ",")
return annotations
}