mirror of
https://github.com/hanzokms/kubernetes-operator.git
synced 2026-10-30 20:30:51 +00:00
101 lines
3.8 KiB
Go
101 lines
3.8 KiB
Go
package kmssecret
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/hanzokms/kubernetes-operator/api/v1alpha1"
|
|
"github.com/hanzokms/kubernetes-operator/internal/util"
|
|
"github.com/go-logr/logr"
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
)
|
|
|
|
func (r *KMSSecretReconciler) SetReadyToSyncSecretsConditions(ctx context.Context, logger logr.Logger, kmsSecret *v1alpha1.KMSSecret, secretsCount int, errorToConditionOn error) {
|
|
if kmsSecret.Status.Conditions == nil {
|
|
kmsSecret.Status.Conditions = []metav1.Condition{}
|
|
}
|
|
|
|
if errorToConditionOn != nil {
|
|
meta.SetStatusCondition(&kmsSecret.Status.Conditions, metav1.Condition{
|
|
Type: "kms.hanzo.ai/ReadyToSyncSecrets",
|
|
Status: metav1.ConditionFalse,
|
|
Reason: "Error",
|
|
Message: fmt.Sprintf("Failed to sync secrets. This can be caused by invalid access token or an invalid API host that is set. Error: %v", errorToConditionOn),
|
|
})
|
|
|
|
meta.SetStatusCondition(&kmsSecret.Status.Conditions, metav1.Condition{
|
|
Type: "kms.hanzo.ai/AutoRedeployReady",
|
|
Status: metav1.ConditionFalse,
|
|
Reason: "Stopped",
|
|
Message: fmt.Sprintf("Auto redeployment has been stopped because the operator failed to sync secrets. Error: %v", errorToConditionOn),
|
|
})
|
|
} else {
|
|
meta.SetStatusCondition(&kmsSecret.Status.Conditions, metav1.Condition{
|
|
Type: "kms.hanzo.ai/ReadyToSyncSecrets",
|
|
Status: metav1.ConditionTrue,
|
|
Reason: "OK",
|
|
Message: fmt.Sprintf("Hanzo KMS controller has started syncing your secrets. Last reconcile synced %d secrets", secretsCount),
|
|
})
|
|
}
|
|
|
|
err := r.Client.Status().Update(ctx, kmsSecret)
|
|
if err != nil {
|
|
logger.Error(err, "Could not set condition for ReadyToSyncSecrets")
|
|
}
|
|
}
|
|
|
|
func (r *KMSSecretReconciler) SetKMSTokenLoadCondition(ctx context.Context, logger logr.Logger, kmsSecret *v1alpha1.KMSSecret, authStrategy util.AuthStrategyType, errorToConditionOn error) {
|
|
if kmsSecret.Status.Conditions == nil {
|
|
kmsSecret.Status.Conditions = []metav1.Condition{}
|
|
}
|
|
|
|
if errorToConditionOn == nil {
|
|
meta.SetStatusCondition(&kmsSecret.Status.Conditions, metav1.Condition{
|
|
Type: "kms.hanzo.ai/LoadedKMSToken",
|
|
Status: metav1.ConditionTrue,
|
|
Reason: "OK",
|
|
Message: fmt.Sprintf("Hanzo KMS controller has loaded the Hanzo KMS token in provided Kubernetes secret, using %v authentication strategy", authStrategy),
|
|
})
|
|
} else {
|
|
meta.SetStatusCondition(&kmsSecret.Status.Conditions, metav1.Condition{
|
|
Type: "kms.hanzo.ai/LoadedKMSToken",
|
|
Status: metav1.ConditionFalse,
|
|
Reason: "Error",
|
|
Message: fmt.Sprintf("Failed to load Hanzo KMS Token from the provided Kubernetes secret because: %v", errorToConditionOn),
|
|
})
|
|
}
|
|
|
|
err := r.Client.Status().Update(ctx, kmsSecret)
|
|
if err != nil {
|
|
logger.Error(err, "Could not set condition for LoadedKMSToken")
|
|
}
|
|
}
|
|
|
|
func (r *KMSSecretReconciler) SetKMSAutoRedeploymentReady(ctx context.Context, logger logr.Logger, kmsSecret *v1alpha1.KMSSecret, numDeployments int, errorToConditionOn error) {
|
|
if kmsSecret.Status.Conditions == nil {
|
|
kmsSecret.Status.Conditions = []metav1.Condition{}
|
|
}
|
|
|
|
if errorToConditionOn == nil {
|
|
meta.SetStatusCondition(&kmsSecret.Status.Conditions, metav1.Condition{
|
|
Type: "kms.hanzo.ai/AutoRedeployReady",
|
|
Status: metav1.ConditionTrue,
|
|
Reason: "OK",
|
|
Message: fmt.Sprintf("Hanzo KMS has found %v deployments which are ready to be auto redeployed when secrets change", numDeployments),
|
|
})
|
|
} else {
|
|
meta.SetStatusCondition(&kmsSecret.Status.Conditions, metav1.Condition{
|
|
Type: "kms.hanzo.ai/AutoRedeployReady",
|
|
Status: metav1.ConditionFalse,
|
|
Reason: "Error",
|
|
Message: fmt.Sprintf("Failed reconcile deployments because: %v", errorToConditionOn),
|
|
})
|
|
}
|
|
|
|
err := r.Client.Status().Update(ctx, kmsSecret)
|
|
if err != nil {
|
|
logger.Error(err, "Could not set condition for AutoRedeployReady")
|
|
}
|
|
}
|