Skip to content

Commit

Permalink
Refactor nodeSelector,tolerations,affinity template variables (#595)
Browse files Browse the repository at this point in the history
Refactor nodeSelector,tolerations,affinity template variables
so that these can be easily modified by cnao.
Added toYaml helper function to render template structs.

Signed-off-by: Radim Hrazdil <[email protected]>
  • Loading branch information
rhrazdil authored Sep 9, 2020
1 parent f1c8aec commit a0d3d56
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 17 deletions.
22 changes: 6 additions & 16 deletions deploy/handler/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@ spec:
name: {{template "handlerPrefix" .}}nmstate-webhook
spec:
serviceAccountName: {{template "handlerPrefix" .}}nmstate-handler
nodeSelector:
beta.kubernetes.io/arch: amd64
node-role.kubernetes.io/master: ""
tolerations:
- key: node-role.kubernetes.io/master
operator: Exists
effect: NoSchedule
nodeSelector: {{ toYaml .WebhookNodeSelector | nindent 8 }}
tolerations: {{ toYaml .WebhookTolerations | nindent 8 }}
affinity: {{ toYaml .WebhookAffinity | nindent 8 }}
containers:
- name: nmstate-webhook
args:
Expand Down Expand Up @@ -111,15 +107,9 @@ spec:
# https://github.com/nmstate/nmstate/pull/440
hostNetwork: true
serviceAccountName: {{template "handlerPrefix" .}}nmstate-handler
nodeSelector:
beta.kubernetes.io/arch: amd64
{{- range $key, $value := .HandlerNodeSelector }}
{{" "}}{{- $key }}: {{ $value }}
{{- end}}
tolerations:
- key: node-role.kubernetes.io/master
operator: Exists
effect: NoSchedule
nodeSelector: {{ toYaml .HandlerNodeSelector | nindent 8 }}
tolerations: {{ toYaml .HandlerTolerations | nindent 8 }}
affinity: {{ toYaml .HandlerAffinity | nindent 8 }}
containers:
- name: nmstate-handler
args:
Expand Down
27 changes: 26 additions & 1 deletion pkg/controller/nmstate/nmstate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (

nmstatev1beta1 "github.com/nmstate/kubernetes-nmstate/pkg/apis/nmstate/v1beta1"
"github.com/nmstate/kubernetes-nmstate/pkg/names"
nmstaterenderer "github.com/nmstate/kubernetes-nmstate/pkg/render"
"github.com/openshift/cluster-network-operator/pkg/apply"
"github.com/openshift/cluster-network-operator/pkg/render"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
uns "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -145,11 +147,34 @@ func (r *ReconcileNMState) applyRBAC(instance *nmstatev1beta1.NMState) error {

func (r *ReconcileNMState) applyHandler(instance *nmstatev1beta1.NMState) error {
data := render.MakeRenderData()
// Register ToYaml template method
data.Funcs["toYaml"] = nmstaterenderer.ToYaml
// Prepare defaults
masterExistsNoScheduleToleration := corev1.Toleration{
Key: "node-role.kubernetes.io/master",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoSchedule,
}
amd64ArchOnMasterNodeSelector := map[string]string{
"beta.kubernetes.io/arch": "amd64",
"node-role.kubernetes.io/master": "",
}
amd64AndCRNodeSelector := instance.Spec.NodeSelector
if amd64AndCRNodeSelector == nil {
amd64AndCRNodeSelector = map[string]string{}
}
amd64AndCRNodeSelector["beta.kubernetes.io/arch"] = "amd64"

data.Data["HandlerNamespace"] = os.Getenv("HANDLER_NAMESPACE")
data.Data["HandlerImage"] = os.Getenv("HANDLER_IMAGE")
data.Data["HandlerPullPolicy"] = os.Getenv("HANDLER_IMAGE_PULL_POLICY")
data.Data["HandlerPrefix"] = os.Getenv("HANDLER_PREFIX")
data.Data["HandlerNodeSelector"] = instance.Spec.NodeSelector
data.Data["WebhookNodeSelector"] = amd64ArchOnMasterNodeSelector
data.Data["WebhookTolerations"] = []corev1.Toleration{masterExistsNoScheduleToleration}
data.Data["WebhookAffinity"] = corev1.Affinity{}
data.Data["HandlerNodeSelector"] = amd64AndCRNodeSelector
data.Data["HandlerTolerations"] = []corev1.Toleration{masterExistsNoScheduleToleration}
data.Data["HandlerAffinity"] = corev1.Affinity{}
// TODO: This is just a place holder to make template renderer happy
// proper variable has to be read from env or CR
data.Data["CARotateInterval"] = ""
Expand Down
20 changes: 20 additions & 0 deletions pkg/render/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package render

import (
"strings"

"sigs.k8s.io/yaml"
)

// toYAML takes an interface, marshals it to yaml, and returns a string. It will
// always return a string, even on marshal error (empty string).
//
// This is designed to be called from a template.
func ToYaml(v interface{}) string {
data, err := yaml.Marshal(v)
if err != nil {
// Swallow errors inside of a template.
return ""
}
return strings.TrimSuffix(string(data), "\n")
}

0 comments on commit a0d3d56

Please sign in to comment.