Skip to content

Commit

Permalink
Merge pull request #287 from peng9808/main
Browse files Browse the repository at this point in the history
Inject dataload-init into the Admission module,Fixed the issue that changing the loop variable does not take effect
  • Loading branch information
peng9808 authored Jul 11, 2024
2 parents d5c3fc4 + 0b4edc2 commit 3ede1ed
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
7 changes: 7 additions & 0 deletions controllers/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,13 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}
}

if newInstance.Spec.AdmissionController.Disable {
err = admissioncontroller.NewAdmissionControllerMaintainer(r.Client, newInstance).Uninstall()
if err != nil {
return ctrl.Result{}, err
}
}

if !newInstance.Spec.AdmissionController.Disable {
newInstance, err = admissioncontroller.NewAdmissionControllerMaintainer(r.Client, newInstance).Ensure()
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion helm/operator/.relok8s-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
- "{{ .global.hwameistorImageRegistry }}/{{ .drbdFocal.imageRepository }}:{{ .ha.drbdVersion }}"
- "{{ .global.hwameistorImageRegistry }}/{{ .dataLoadManager.imageRepository }}:{{ .dataLoadManager.tag }}"
- "{{ .global.hwameistorImageRegistry }}/{{ .dataSetManager.imageRepository }}:{{ .dataSetManager.tag }}"

- "{{ .global.hwameistorImageRegistry }}/{{ .dataLoadInit.imageRepository }}:{{ .dataLoadInit.tag }}"
# - "{{ .global.hwameistorImageRegistry }}/{{ .ha.imageRepoOwner }}/drbd9-jammy:v9.1.11"
3 changes: 3 additions & 0 deletions helm/operator/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ dataSetManager:
imageRepository: hwameistor/dataset-manager
tag: v0.0.1

dataLoadInit:
imageRepository: hwameistor/dataload-init
tag: v0.0.1

#datasetDefaultPoolClass: NVMe > SSD > HDD

Expand Down
55 changes: 55 additions & 0 deletions pkg/install/admissioncontroller/admission_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package admissioncontroller
import (
"context"
"errors"
"fmt"
"reflect"
"time"

Expand Down Expand Up @@ -133,6 +134,10 @@ func setAdmissionControllerContainers(clusterInstance *hwameistoriov1alpha1.Clus
}
imageSpec := clusterInstance.Spec.AdmissionController.Controller.Image
container.Image = imageSpec.Registry + "/" + imageSpec.Repository + ":" + imageSpec.Tag

dataLoadManager := clusterInstance.Spec.DataLoadManager.DataLoadManagerContainer.Image
dataloadImageValue := dataLoadManager.Registry + "/" + "hwameistor/dataload-init" + ":" + dataLoadManager.Tag

container.Env = append(container.Env, []corev1.EnvVar{
{
Name: "WEBHOOK_NAMESPACE",
Expand All @@ -142,6 +147,10 @@ func setAdmissionControllerContainers(clusterInstance *hwameistoriov1alpha1.Clus
Name: "FAILURE_POLICY",
Value: clusterInstance.Spec.AdmissionController.FailurePolicy,
},
{
Name: "DATALOADER_IMAGE",
Value: dataloadImageValue,
},
}...)
}
admissionController.Spec.Template.Spec.Containers[i] = container
Expand Down Expand Up @@ -169,6 +178,22 @@ func needOrNotToUpdateAdmissionController(cluster *hwameistoriov1alpha1.Cluster,
admissionControllerToUpdate.Spec.Template.Spec.Containers[i] = container
needToUpdate = true
}
for k, envVar := range container.Env {
if envVar.Name == "FAILURE_POLICY" {
if envVar.Value != cluster.Spec.AdmissionController.FailurePolicy {
admissionControllerToUpdate.Spec.Template.Spec.Containers[i].Env[k].Value = cluster.Spec.AdmissionController.FailurePolicy
needToUpdate = true
}
}
if envVar.Name == "DATALOADER_IMAGE" {
dataLoadManager := cluster.Spec.DataLoadManager.DataLoadManagerContainer.Image
dataloadImageValue := dataLoadManager.Registry + "/" + "hwameistor/dataload-init" + ":" + dataLoadManager.Tag
if envVar.Value != dataloadImageValue {
admissionControllerToUpdate.Spec.Template.Spec.Containers[i].Env[k].Value = dataloadImageValue
needToUpdate = true
}
}
}
}
}

Expand Down Expand Up @@ -273,6 +298,36 @@ func (m *AdmissionControllerMaintainer) Ensure() (*hwameistoriov1alpha1.Cluster,
return newClusterInstance, nil
}

func (m *AdmissionControllerMaintainer) Uninstall() error {
key := types.NamespacedName{
Namespace: admissionController.Namespace,
Name: admissionController.Name,
}
var gottenAdmissionController appsv1.Deployment
if err := m.Client.Get(context.TODO(), key, &gottenAdmissionController); err != nil {
if apierrors.IsNotFound(err) {
log.WithError(err)
return nil
} else {
log.Errorf("Get AdmissionController err: %v", err)
return err
}
} else {
for _, reference := range gottenAdmissionController.OwnerReferences {
if reference.Name == m.ClusterInstance.Name {
if err = m.Client.Delete(context.TODO(), &gottenAdmissionController); err != nil {
return err
} else {
return nil
}
}
}
}
err := fmt.Errorf("Admission Owner is not %s", m.ClusterInstance.Name)
log.WithError(err)
return nil
}

// ensure hwameistor-admission-ca is generated before the admission controller running
func (m *AdmissionControllerMaintainer) ensureAdmissionCA() error {
// skip generation if the ca already exists
Expand Down

0 comments on commit 3ede1ed

Please sign in to comment.