Skip to content

Commit

Permalink
Merge pull request #575 from WASdev/update-ltpa-labels
Browse files Browse the repository at this point in the history
Update LTPA labels + misc updates
  • Loading branch information
leochr authored Nov 2, 2023
2 parents 3ba6d10 + b8efb3e commit f118d31
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 23 deletions.
30 changes: 13 additions & 17 deletions controllers/ltpa_keys_sharing.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ import (
)

// Create the Deployment and Service objects for a Semeru Compiler used by a Websphere Liberty Application
func (r *ReconcileWebSphereLiberty) reconcileLTPAKeysSharing(instance *wlv1.WebSphereLibertyApplication, defaultMeta metav1.ObjectMeta) (error, string, string) {
func (r *ReconcileWebSphereLiberty) reconcileLTPAKeysSharing(instance *wlv1.WebSphereLibertyApplication) (error, string, string) {
var ltpaSecretName string
var err error
if r.isLTPAKeySharingEnabled(instance) {
err, ltpaSecretName = r.generateLTPAKeys(instance, defaultMeta)
err, ltpaSecretName = r.generateLTPAKeys(instance)
if err != nil {
return err, "Failed to generate the shared LTPA Keys file", ltpaSecretName
}
} else {
err := r.deleteLTPAKeysResources(instance, defaultMeta)
err := r.deleteLTPAKeysResources(instance)
if err != nil {
return err, "Failed to delete LTPA Keys Resource", ltpaSecretName
}
Expand All @@ -58,7 +58,7 @@ func (r *ReconcileWebSphereLiberty) getOrSetLTPAKeysSharingLeader(instance *wlv1
ltpaServiceAccount := &corev1.ServiceAccount{}
ltpaServiceAccount.Name = OperatorShortName + "-ltpa"
ltpaServiceAccount.Namespace = instance.GetNamespace()
ltpaServiceAccount.Labels = instance.GetLabels()
ltpaServiceAccount.Labels = lutils.GetRequiredLabels(ltpaServiceAccount.Name, "")
err := r.GetClient().Get(context.TODO(), types.NamespacedName{Name: ltpaServiceAccount.Name, Namespace: ltpaServiceAccount.Namespace}, ltpaServiceAccount)
if err != nil {
if kerrors.IsNotFound(err) {
Expand Down Expand Up @@ -105,7 +105,7 @@ func (r *ReconcileWebSphereLiberty) restartLTPAKeysGeneration(instance *wlv1.Web
}

// Generates the LTPA keys file and returns the name of the Secret storing its metadata
func (r *ReconcileWebSphereLiberty) generateLTPAKeys(instance *wlv1.WebSphereLibertyApplication, defaultMeta metav1.ObjectMeta) (error, string) {
func (r *ReconcileWebSphereLiberty) generateLTPAKeys(instance *wlv1.WebSphereLibertyApplication) (error, string) {
// Don't generate LTPA keys if this instance is not the leader
err, ltpaKeySharingLeaderName, isLTPAKeySharingLeader, ltpaServiceAccountName := r.getOrSetLTPAKeysSharingLeader(instance)
if err != nil {
Expand All @@ -116,29 +116,29 @@ func (r *ReconcileWebSphereLiberty) generateLTPAKeys(instance *wlv1.WebSphereLib
ltpaXMLSecret := &corev1.Secret{}
ltpaXMLSecret.Name = OperatorShortName + lutils.LTPAServerXMLSuffix
ltpaXMLSecret.Namespace = instance.GetNamespace()
ltpaXMLSecret.Labels = instance.GetLabels()
ltpaXMLSecret.Labels = lutils.GetRequiredLabels(ltpaXMLSecret.Name, "")

generateLTPAKeysJob := &v1.Job{}
generateLTPAKeysJob.Name = OperatorShortName + "-managed-ltpa-keys-generation"
generateLTPAKeysJob.Namespace = instance.GetNamespace()
generateLTPAKeysJob.Labels = instance.GetLabels()
generateLTPAKeysJob.Labels = lutils.GetRequiredLabels(generateLTPAKeysJob.Name, "")

deletePropagationBackground := metav1.DeletePropagationBackground

ltpaJobRequest := &corev1.ConfigMap{}
ltpaJobRequest.Name = OperatorShortName + "-managed-ltpa-job-request"
ltpaJobRequest.Namespace = instance.GetNamespace()
ltpaJobRequest.Labels = instance.GetLabels()
ltpaJobRequest.Labels = lutils.GetRequiredLabels(ltpaJobRequest.Name, "")

ltpaKeysCreationScriptConfigMap := &corev1.ConfigMap{}
ltpaKeysCreationScriptConfigMap.Name = OperatorShortName + "-managed-ltpa-script"
ltpaKeysCreationScriptConfigMap.Namespace = instance.GetNamespace()
ltpaKeysCreationScriptConfigMap.Labels = instance.GetLabels()
ltpaKeysCreationScriptConfigMap.Labels = lutils.GetRequiredLabels(ltpaKeysCreationScriptConfigMap.Name, "")

ltpaSecret := &corev1.Secret{}
ltpaSecret.Name = OperatorShortName + "-managed-ltpa"
ltpaSecret.Namespace = instance.GetNamespace()
ltpaSecret.Labels = instance.GetLabels()
ltpaSecret.Labels = lutils.GetRequiredLabels(ltpaSecret.Name, "")
// If the LTPA Secret does not exist, run the Kubernetes Job to generate the shared ltpa.keys file and Secret
err = r.GetClient().Get(context.TODO(), types.NamespacedName{Name: ltpaSecret.Name, Namespace: ltpaSecret.Namespace}, ltpaSecret)
if err != nil && kerrors.IsNotFound(err) {
Expand Down Expand Up @@ -185,7 +185,7 @@ func (r *ReconcileWebSphereLiberty) generateLTPAKeys(instance *wlv1.WebSphereLib
Resources: []string{"secrets"},
},
}
ltpaRole.Labels = instance.GetLabels()
ltpaRole.Labels = lutils.GetRequiredLabels(ltpaRole.Name, "")
r.CreateOrUpdate(ltpaRole, instance, func() error {
return nil
})
Expand All @@ -205,16 +205,12 @@ func (r *ReconcileWebSphereLiberty) generateLTPAKeys(instance *wlv1.WebSphereLib
Kind: "Role",
Name: ltpaRole.Name,
}
ltpaRoleBinding.Labels = instance.GetLabels()
ltpaRoleBinding.Labels = lutils.GetRequiredLabels(ltpaRoleBinding.Name, "")
r.CreateOrUpdate(ltpaRoleBinding, instance, func() error {
return nil
})

// Create a ConfigMap to store the controllers/assets/create_ltpa_keys.sh script
ltpaKeysCreationScriptConfigMap := &corev1.ConfigMap{}
ltpaKeysCreationScriptConfigMap.Name = OperatorShortName + "-managed-ltpa-script"
ltpaKeysCreationScriptConfigMap.Namespace = instance.GetNamespace()
ltpaKeysCreationScriptConfigMap.Labels = instance.GetLabels()
err = r.GetClient().Get(context.TODO(), types.NamespacedName{Name: ltpaKeysCreationScriptConfigMap.Name, Namespace: ltpaKeysCreationScriptConfigMap.Namespace}, ltpaKeysCreationScriptConfigMap)
if err != nil && kerrors.IsNotFound(err) {
ltpaKeysCreationScriptConfigMap.Data = make(map[string]string)
Expand Down Expand Up @@ -291,7 +287,7 @@ func (r *ReconcileWebSphereLiberty) isLTPAKeySharingEnabled(instance *wlv1.WebSp
}

// Deletes resources used to create the LTPA keys file
func (r *ReconcileWebSphereLiberty) deleteLTPAKeysResources(instance *wlv1.WebSphereLibertyApplication, defaultMeta metav1.ObjectMeta) error {
func (r *ReconcileWebSphereLiberty) deleteLTPAKeysResources(instance *wlv1.WebSphereLibertyApplication) error {
// Don't delete LTPA keys resources if this instance is not the leader
err, _, isLTPAKeySharingLeader, ltpaServiceAccountName := r.getOrSetLTPAKeysSharingLeader(instance)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion controllers/semeru_compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ func (r *ReconcileWebSphereLiberty) areSemeruCompilerResourcesReady(wlva *wlv1.W
}

func (r *ReconcileWebSphereLiberty) isSemeruEnabled(wlva *wlv1.WebSphereLibertyApplication) bool {
if wlva.GetSemeruCloudCompiler() != nil && wlva.GetSemeruCloudCompiler().Enable == true {
if wlva.GetSemeruCloudCompiler() != nil && wlva.GetSemeruCloudCompiler().Enable {
return true
} else {
return false
Expand Down
2 changes: 1 addition & 1 deletion controllers/webspherelibertyapplication_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ func (r *ReconcileWebSphereLiberty) Reconcile(ctx context.Context, request ctrl.
return r.ManageError(err, common.StatusConditionTypeReconciled, instance)
}

err, message, ltpaSecretName := r.reconcileLTPAKeysSharing(instance, defaultMeta)
err, message, ltpaSecretName := r.reconcileLTPAKeysSharing(instance)
if err != nil {
reqLogger.Error(err, message)
return r.ManageError(err, common.StatusConditionTypeReconciled, instance)
Expand Down
7 changes: 3 additions & 4 deletions controllers/webspherelibertytrace_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"github.com/go-logr/logr"

webspherelibertyv1 "github.com/WASdev/websphere-liberty-operator/api/v1"
"github.com/WASdev/websphere-liberty-operator/utils"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -140,7 +139,7 @@ func (r *ReconcileWebSphereLibertyTrace) Reconcile(ctx context.Context, request
if instance.Spec.Disable != nil && *instance.Spec.Disable {
//Disable trace if trace was previously enabled on the same pod
if !podChanged && prevTraceEnabled == corev1.ConditionTrue {
_, err = utils.ExecuteCommandInContainer(r.RestConfig, podName, podNamespace, "app", []string{"/bin/sh", "-c", "rm -f " + traceConfigFile})
_, err = lutils.ExecuteCommandInContainer(r.RestConfig, podName, podNamespace, "app", []string{"/bin/sh", "-c", "rm -f " + traceConfigFile})
if err != nil {
reqLogger.Error(err, "Encountered error while disabling trace for pod "+podName+" in namespace "+podNamespace)
return r.UpdateStatus(err, webspherelibertyv1.OperationStatusConditionTypeEnabled, *instance, corev1.ConditionTrue, podName, podChanged)
Expand All @@ -159,7 +158,7 @@ func (r *ReconcileWebSphereLibertyTrace) Reconcile(ctx context.Context, request
}
traceConfig += "/></server>"

_, err = utils.ExecuteCommandInContainer(r.RestConfig, podName, podNamespace, "app", []string{"/bin/sh", "-c", "mkdir -p " + traceOutputDir + " && echo '" + traceConfig + "' > " + traceConfigFile})
_, err = lutils.ExecuteCommandInContainer(r.RestConfig, podName, podNamespace, "app", []string{"/bin/sh", "-c", "mkdir -p " + traceOutputDir + " && echo '" + traceConfig + "' > " + traceConfigFile})
if err != nil {
reqLogger.Error(err, "Encountered error while setting up trace for pod "+podName+" in namespace "+podNamespace)
return r.UpdateStatus(err, webspherelibertyv1.OperationStatusConditionTypeEnabled, *instance, corev1.ConditionFalse, podName, podChanged)
Expand Down Expand Up @@ -229,7 +228,7 @@ func (r *ReconcileWebSphereLibertyTrace) disableTraceOnPrevPod(reqLogger logr.Lo
reqLogger.Info("Previous pod " + prevPodName + " was not found in namespace " + podNamespace)
} else {
//Stop tracing on previous Pod
_, err = utils.ExecuteCommandInContainer(r.RestConfig, prevPodName, podNamespace, "app", []string{"/bin/sh", "-c", "rm -f " + traceConfigFile})
_, err = lutils.ExecuteCommandInContainer(r.RestConfig, prevPodName, podNamespace, "app", []string{"/bin/sh", "-c", "rm -f " + traceConfigFile})
if err == nil {
reqLogger.Info("Disabled trace on previous pod " + prevPodName + " in namespace " + podNamespace)
} else {
Expand Down
12 changes: 12 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -718,3 +718,15 @@ func GetLTPAXMLVolumeMount(la *wlv1.WebSphereLibertyApplication, fileName string
SubPath: fileName,
}
}

func GetRequiredLabels(name string, instance string) map[string]string {
requiredLabels := make(map[string]string)
requiredLabels["app.kubernetes.io/name"] = name
if instance != "" {
requiredLabels["app.kubernetes.io/instance"] = instance
} else {
requiredLabels["app.kubernetes.io/instance"] = name
}
requiredLabels["app.kubernetes.io/managed-by"] = "websphere-liberty-operator"
return requiredLabels
}

0 comments on commit f118d31

Please sign in to comment.