diff --git a/pkg/metallb/metallb.go b/pkg/metallb/metallb.go index 9faef005a..c142af16c 100644 --- a/pkg/metallb/metallb.go +++ b/pkg/metallb/metallb.go @@ -3,6 +3,7 @@ package metallb import ( "context" "fmt" + corev1 "k8s.io/api/core/v1" "github.com/golang/glog" "github.com/openshift-kni/eco-goinfra/pkg/clients" @@ -314,6 +315,163 @@ func (builder *Builder) WithSpeakerNodeSelector(label map[string]string) *Builde return builder } +// WithMetallbControllerConfig defines the controller config with a helm chart table. +func (builder *Builder) WithMetallbControllerConfig(priorityClassName, runtimeClassName, deploymentName, + helmChartControllerTestName, helmChartKeyExample string) *Builder { + if valid, _ := builder.validate(); !valid { + return builder + } + + if !builder.Exists() { + glog.V(100).Infof("failed to update metallb, object doesn't exist on cluster") + } + + glog.V(100).Infof("Defining the metallb object with Helm Chart in namespace %s", + builder.Definition.Namespace, + ) + + if priorityClassName == "" { + glog.V(100).Infof("The controller config priorityClassName is empty") + + builder.errorMsg = "controller config priorityClassName cannot be empty" + } + + if runtimeClassName == "" { + glog.V(100).Infof("The controller config runtimeClassName is empty") + + builder.errorMsg = "controller config runtimeClassName cannot be empty" + } + + if deploymentName == "" { + glog.V(100).Infof("The controller config deploymentName is empty") + + builder.errorMsg = "controller config deploymentName cannot be empty" + } + + if helmChartControllerTestName == "" { + glog.V(100).Infof("The controller config helmChartControllerTestName is empty") + + builder.errorMsg = "controller config helmChartControllerTestName cannot be empty" + } + + if helmChartKeyExample == "" { + glog.V(100).Infof("The controller config helmChartKeyExample is empty") + + builder.errorMsg = "controller config helmChartKeyExample cannot be empty" + } + + if builder.errorMsg != "" { + glog.V(100).Infof("Error occurred building metallb Controller Config") + } + builder.Definition.Spec = mlbtypes.MetalLBSpec{ + ControllerConfig: &mlbtypes.Config{ + PriorityClassName: priorityClassName, + RuntimeClassName: runtimeClassName, + Annotations: map[string]string{deploymentName: "controller"}, + Affinity: &corev1.Affinity{ + PodAffinity: &corev1.PodAffinity{ + RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{ + {LabelSelector: &metaV1.LabelSelector{ + MatchLabels: map[string]string{"component": helmChartControllerTestName}, + }, + TopologyKey: "kubernetes.io/hostname", + }, + }, + }, + }, + }, + ControllerTolerations: []corev1.Toleration{ + { + Key: helmChartKeyExample, + Operator: "exist", + Effect: "NoExecute"}, + }, + } + + return builder +} + +// WithMetallbSpeakerConfig defines the speaker config with a helm chart table. +func (builder *Builder) WithMetallbSpeakerConfig(speakerPriorityClassName, runtimeClassName, helmChartDemoSpeakerName, + helmChartSpeakerTestName, keyName string) *Builder { + if valid, err := builder.validate(); !valid { + if err != nil { + glog.V(100).Infof("Error occurred validating the metallb Speaker Config") + } + + return builder + } + + if !builder.Exists() { + glog.V(100).Infof("failed to update metallb, object doesn't exist on cluster") + } + + glog.V(100).Infof("Defining the metallb object with Helm Chart in namespace %s", + builder.Definition.Namespace, + ) + + if speakerPriorityClassName == "" { + glog.V(100).Infof("The speaker config speakerPriorityClassName is empty") + + builder.errorMsg = "speaker config speakerPriorityClassName cannot be empty" + } + + if runtimeClassName == "" { + glog.V(100).Infof("The speaker config runtimeClassName is empty") + + builder.errorMsg = "speaker config runtimeClassName cannot be empty" + } + + if helmChartDemoSpeakerName == "" { + glog.V(100).Infof("The speaker config helmChartDemoSpeakerName is empty") + + builder.errorMsg = "speaker config helmChartDemoSpeakerName cannot be empty" + } + + if helmChartSpeakerTestName == "" { + glog.V(100).Infof("The speaker config helmChartSpeakerTestName is empty") + + builder.errorMsg = "speaker config helmChartSpeakerTestName cannot be empty" + } + + if keyName == "" { + glog.V(100).Infof("The speaker config keyName is empty") + + builder.errorMsg = "speaker config keyName cannot be empty" + } + + if builder.errorMsg != "" { + glog.V(100).Infof("Error occurred building metallb Controller Config") + } + + builder.Definition.Spec = mlbtypes.MetalLBSpec{ + SpeakerConfig: &mlbtypes.Config{PriorityClassName: speakerPriorityClassName, RuntimeClassName: runtimeClassName, + Annotations: map[string]string{"controller": helmChartDemoSpeakerName}, + Affinity: &corev1.Affinity{PodAffinity: &corev1.PodAffinity{ + RequiredDuringSchedulingIgnoredDuringExecution: []corev1.PodAffinityTerm{ + {LabelSelector: &metaV1.LabelSelector{ + MatchLabels: map[string]string{ + "component": helmChartSpeakerTestName, + }, + }, + TopologyKey: "kubernetes.io/hostname", + }, + }, + }, + }, + }, + SpeakerTolerations: []corev1.Toleration{ + { + Key: keyName, + Operator: "Exists", + Effect: "NoExecute", + }, + }, + } + + return builder +} + // WithOptions creates metallb with generic mutation options. func (builder *Builder) WithOptions(options ...AdditionalOptions) *Builder { if valid, _ := builder.validate(); !valid {