This repository has been archived by the owner on Oct 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
k8s_secret.go
194 lines (161 loc) · 5.87 KB
/
k8s_secret.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 entrypoints
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"k8s.io/client-go/rest"
"github.com/flyteorg/flytestdlib/logger"
kubeErrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/flyteorg/flyteadmin/auth"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/flyteorg/flyteadmin/pkg/config"
executioncluster "github.com/flyteorg/flyteadmin/pkg/executioncluster/impl"
"github.com/flyteorg/flyteadmin/pkg/executioncluster/interfaces"
"github.com/flyteorg/flyteadmin/pkg/runtime"
"github.com/flyteorg/flytestdlib/errors"
"github.com/flyteorg/flytestdlib/promutils"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"k8s.io/client-go/kubernetes"
)
const (
PodNamespaceEnvVar = "POD_NAMESPACE"
podDefaultNamespace = "default"
)
var (
secretName string
secretsLocalPath string
forceUpdate bool
)
var secretsCmd = &cobra.Command{
Use: "secret",
Aliases: []string{"secrets"},
}
var secretsPersistCmd = &cobra.Command{
Use: "create",
Long: `Creates a new secret (or noop if one exists unless --force is provided) using keys found in the provided path.
If POD_NAMESPACE env var is set, the secret will be created in that namespace.
`,
Example: `
Create a secret using default name (flyte-admin-auth) in default namespace
flyteadmin secret create --fromPath=/path/in/container
Override an existing secret if one exists (reads secrets from default path /etc/secrets/):
flyteadmin secret create --name "my-auth-secrets" --force
`,
RunE: func(cmd *cobra.Command, args []string) error {
return persistSecrets(context.Background(), cmd.Flags())
},
}
func init() {
secretsPersistCmd.Flags().StringVar(&secretName, "name", "flyte-admin-auth", "Chooses secret name to create/update")
secretsPersistCmd.Flags().StringVar(&secretsLocalPath, "fromPath", filepath.Join(string(os.PathSeparator), "etc", "secrets"), "Chooses secret name to create/update")
secretsPersistCmd.Flags().BoolVarP(&forceUpdate, "force", "f", false, "Whether to update the secret if one exists")
secretsCmd.AddCommand(secretsPersistCmd)
secretsCmd.AddCommand(auth.GetInitSecretsCommand())
RootCmd.AddCommand(secretsCmd)
}
func buildK8sSecretData(_ context.Context, localPath string) (map[string][]byte, error) {
secretsData := make(map[string][]byte, 4)
err := filepath.Walk(localPath, func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() {
return nil
}
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
secretsData[strings.TrimPrefix(path, filepath.Dir(path)+string(filepath.Separator))] = data
return nil
})
if err != nil {
return nil, err
}
return secretsData, nil
}
func persistSecrets(ctx context.Context, _ *pflag.FlagSet) error {
configuration := runtime.NewConfigurationProvider()
scope := promutils.NewScope(configuration.ApplicationConfiguration().GetTopLevelConfig().MetricsScope)
initializationErrorCounter := scope.NewSubScope("secrets").MustNewCounter(
"flyteclient_initialization_error",
"count of errors encountered initializing a flyte client from kube config")
var listTargetsProvider interfaces.ListTargetsInterface
var err error
if len(configuration.ClusterConfiguration().GetClusterConfigs()) == 0 {
serverConfig := config.GetConfig()
listTargetsProvider, err = executioncluster.NewInCluster(initializationErrorCounter, serverConfig.KubeConfig, serverConfig.Master)
} else {
listTargetsProvider, err = executioncluster.NewListTargets(initializationErrorCounter, executioncluster.NewExecutionTargetProvider(), configuration.ClusterConfiguration())
}
if err != nil {
return err
}
targets := listTargetsProvider.GetValidTargets()
// Since we are targeting the cluster Admin is running in, this list should contain exactly one item
if len(targets) != 1 {
return fmt.Errorf("expected exactly 1 valid target cluster. Found [%v]", len(targets))
}
var clusterCfg rest.Config
for _, target := range targets {
// We've just ascertained targets contains exactly 1 item, so we can safely assume we'll assign the clusterCfg
// from that one item now.
clusterCfg = target.Config
}
kubeClient, err := kubernetes.NewForConfig(&clusterCfg)
if err != nil {
return errors.Wrapf("INIT", err, "Error building kubernetes clientset")
}
podNamespace, found := os.LookupEnv(PodNamespaceEnvVar)
if !found {
podNamespace = podDefaultNamespace
}
secretsData, err := buildK8sSecretData(ctx, secretsLocalPath)
if err != nil {
return errors.Wrapf("INIT", err, "Error building k8s secret's data field.")
}
secretsClient := kubeClient.CoreV1().Secrets(podNamespace)
newSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: podNamespace,
},
Type: corev1.SecretTypeOpaque,
Data: secretsData,
}
_, err = secretsClient.Create(ctx, newSecret, metav1.CreateOptions{})
if err != nil && kubeErrors.IsAlreadyExists(err) {
if forceUpdate {
logger.Infof(ctx, "A secret already exists with the same name. Attempting to update it.")
_, err = secretsClient.Update(ctx, newSecret, metav1.UpdateOptions{})
} else {
var existingSecret *corev1.Secret
existingSecret, err = secretsClient.Get(ctx, newSecret.Name, metav1.GetOptions{})
if err != nil {
logger.Infof(ctx, "Failed to retrieve existing secret. Error: %v", err)
return err
}
if existingSecret.Data == nil {
existingSecret.Data = map[string][]byte{}
}
needsUpdate := false
for key, val := range secretsData {
if _, found := existingSecret.Data[key]; !found {
existingSecret.Data[key] = val
needsUpdate = true
}
}
if needsUpdate {
_, err = secretsClient.Update(ctx, existingSecret, metav1.UpdateOptions{})
if err != nil && kubeErrors.IsConflict(err) {
logger.Infof(ctx, "Another instance of flyteadmin has updated the same secret. Ignoring this update")
err = nil
}
}
}
return err
}
return err
}