saveSecret creates a secret when it does not exist, otherwise updates it.
(ctx context.Context, secret *corev1.Secret, ns string)
| 32 | |
| 33 | // saveSecret creates a secret when it does not exist, otherwise updates it. |
| 34 | func SaveSecret(ctx context.Context, secret *corev1.Secret, ns string) error { |
| 35 | cli := GetKubeClient() |
| 36 | if err := CreateNamespaceIfNeeded(ctx, ns); err != nil { |
| 37 | return fmt.Errorf("failed to create Namespace kubeedge, error: %v", err) |
| 38 | } |
| 39 | if _, err := cli.CoreV1().Secrets(ns).Create(ctx, secret, metav1.CreateOptions{}); err != nil { |
| 40 | if errors.IsAlreadyExists(err) { |
| 41 | if _, err := cli.CoreV1().Secrets(ns).Update(ctx, secret, metav1.UpdateOptions{}); err != nil { |
| 42 | return fmt.Errorf("failed to update the secret, namespace: %s, name: %s, err: %v", ns, secret.Name, err) |
| 43 | } |
| 44 | } else { |
| 45 | return fmt.Errorf("failed to create the secret, namespace: %s, name: %s, err: %v", ns, secret.Name, err) |
| 46 | } |
| 47 | } |
| 48 | return nil |
| 49 | } |