-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35db9c4
commit 895b8a7
Showing
2 changed files
with
59 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package stacks | ||
|
||
import ( | ||
"context" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
apierrs "k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"sigs.k8s.io/controller-runtime/pkg/log" | ||
) | ||
|
||
const ( | ||
secretName = "job-run-secret" | ||
secretDeployTokenKey = "deploy-token" | ||
) | ||
|
||
func (r *StackReconciler) upsertRunSecret(ctx context.Context) (*corev1.Secret, error) { | ||
logger := log.FromContext(ctx) | ||
secret := &corev1.Secret{} | ||
|
||
if err := r.k8sClient.Get(ctx, types.NamespacedName{Name: secretName, Namespace: r.namespace}, secret); err != nil { | ||
if !apierrs.IsNotFound(err) { | ||
return nil, err | ||
} | ||
|
||
logger.V(2).Info("generating secret", "namespace", r.namespace, "name", secretName) | ||
secret = &corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{Name: secretName, Namespace: r.namespace}, | ||
StringData: map[string]string{secretDeployTokenKey: r.deployToken}, | ||
} | ||
|
||
logger.V(2).Info("creating secret", "namespace", secret.Namespace, "name", secret.Name) | ||
if err := r.k8sClient.Create(ctx, secret); err != nil { | ||
logger.Error(err, "unable to create secret") | ||
return nil, err | ||
} | ||
|
||
return secret, nil | ||
} | ||
|
||
if deployToken, exists := secret.Data[secretDeployTokenKey]; !exists || string(deployToken) != r.deployToken { | ||
logger.V(2).Info("updating secret", "namespace", secret.Namespace, "name", secret.Name) | ||
secret.StringData = map[string]string{secretDeployTokenKey: r.deployToken} | ||
if err := r.k8sClient.Update(ctx, secret); err != nil { | ||
logger.Error(err, "unable to update secret") | ||
return nil, err | ||
} | ||
} | ||
|
||
return secret, nil | ||
|
||
} |