CreateSecret is used to upsert secret
(ctx context.Context, projectID string, secretObj *model.Secret)
| 18 | |
| 19 | // CreateSecret is used to upsert secret |
| 20 | func (i *Istio) CreateSecret(ctx context.Context, projectID string, secretObj *model.Secret) error { |
| 21 | // check whether the oldSecret type is correct! |
| 22 | if secretObj.Type != model.FileType && secretObj.Type != model.EnvType && secretObj.Type != model.DockerType { |
| 23 | return helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Invalid old secret type (%s) provided", secretObj.Type), nil, nil) |
| 24 | } |
| 25 | |
| 26 | oldSecret, err := i.kube.CoreV1().Secrets(projectID).Get(ctx, secretObj.ID, metav1.GetOptions{}) |
| 27 | if kubeErrors.IsNotFound(err) { |
| 28 | // Create a new Secret |
| 29 | helpers.Logger.LogDebug(helpers.GetRequestID(ctx), fmt.Sprintf("Creating secret (%s)", secretObj.ID), nil) |
| 30 | newSecret, err := generateSecret(ctx, projectID, secretObj) |
| 31 | if err != nil { |
| 32 | return err |
| 33 | } |
| 34 | |
| 35 | _, err = i.kube.CoreV1().Secrets(projectID).Create(ctx, newSecret, metav1.CreateOptions{}) |
| 36 | return err |
| 37 | |
| 38 | } else if err == nil { |
| 39 | // oldSecret already exists...update it! |
| 40 | helpers.Logger.LogDebug(helpers.GetRequestID(ctx), fmt.Sprintf("Updating secret (%s)", secretObj.ID), nil) |
| 41 | oldSecretType := oldSecret.Annotations["secretType"] |
| 42 | if oldSecret.Annotations["secretType"] != secretObj.Type { |
| 43 | return helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Secret type mismatch. Wanted - %s; Got - %s", oldSecretType, secretObj.Type), nil, nil) |
| 44 | } |
| 45 | newSecret, err := generateSecret(ctx, projectID, secretObj) |
| 46 | if err != nil { |
| 47 | return err |
| 48 | } |
| 49 | _, err = i.kube.CoreV1().Secrets(projectID).Update(ctx, newSecret, metav1.UpdateOptions{}) |
| 50 | return err |
| 51 | } |
| 52 | return helpers.Logger.LogError(helpers.GetRequestID(ctx), fmt.Sprintf("Failed to create secret (%s)", secretObj.ID), err, nil) |
| 53 | } |
| 54 | |
| 55 | // ListSecrets lists all the secrets in the provided name-space! |
| 56 | func (i *Istio) ListSecrets(ctx context.Context, projectID string) ([]*model.Secret, error) { |
nothing calls this directly
no test coverage detected