Fix linting issues

Also fixes nodelabeler compile issue

Change-Id: I49e7c27318029f4033a177633becaafb1098eff3
This commit is contained in:
Kostiantyn Kalynovskyi 2021-01-12 16:37:42 -06:00
parent ced31134ef
commit 060dfdff3b
3 changed files with 43 additions and 22 deletions

View File

@ -112,7 +112,7 @@ func (r *VinoReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
} }
vino.Status.ConfigMapReady = true vino.Status.ConfigMapReady = true
err = r.ensureDaemonSet(ctx, req.NamespacedName, vino) err = r.ensureDaemonSet(ctx, vino)
if err != nil { if err != nil {
err = fmt.Errorf("Could not reconcile Daemonset: %w", err) err = fmt.Errorf("Could not reconcile Daemonset: %w", err)
readyCondition := metav1.Condition{ readyCondition := metav1.Condition{
@ -134,7 +134,6 @@ func (r *VinoReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.
} }
func (r *VinoReconciler) ensureConfigMap(ctx context.Context, name types.NamespacedName, vino *vinov1.Vino) error { func (r *VinoReconciler) ensureConfigMap(ctx context.Context, name types.NamespacedName, vino *vinov1.Vino) error {
logger := r.Log.WithValues("vino", name) logger := r.Log.WithValues("vino", name)
generatedCM, err := r.buildConfigMap(name, vino) generatedCM, err := r.buildConfigMap(name, vino)
@ -225,8 +224,7 @@ func needsUpdate(generated, current *corev1.ConfigMap) bool {
return false return false
} }
func (r *VinoReconciler) ensureDaemonSet(ctx context.Context, name types.NamespacedName, vino *vinov1.Vino) error { func (r *VinoReconciler) ensureDaemonSet(ctx context.Context, vino *vinov1.Vino) error {
ds, err := r.overrideDaemonSet(ctx, vino) ds, err := r.overrideDaemonSet(ctx, vino)
if err != nil { if err != nil {
return err return err
@ -238,7 +236,11 @@ func (r *VinoReconciler) ensureDaemonSet(ctx context.Context, name types.Namespa
r.decorateDaemonSet(ds, vino) r.decorateDaemonSet(ds, vino)
if err := applyRuntimeObject(ctx, types.NamespacedName{Name: ds.Name, Namespace: ds.Namespace}, ds, r.Client); err != nil { if err := applyRuntimeObject(
ctx,
types.NamespacedName{Name: ds.Name, Namespace: ds.Namespace},
ds,
r.Client); err != nil {
return err return err
} }
@ -288,7 +290,8 @@ func (r *VinoReconciler) decorateDaemonSet(ds *appsv1.DaemonSet, vino *vinov1.Vi
"vino instance", vino.Namespace+"/"+vino.Name, "vino instance", vino.Namespace+"/"+vino.Name,
"container name", c.Name, "container name", c.Name,
) )
ds.Spec.Template.Spec.Containers[i].VolumeMounts = append(c.VolumeMounts, corev1.VolumeMount{ ds.Spec.Template.Spec.Containers[i].VolumeMounts = append(
ds.Spec.Template.Spec.Containers[i].VolumeMounts, corev1.VolumeMount{
MountPath: "/vino/spec", MountPath: "/vino/spec",
Name: volume, Name: volume,
ReadOnly: true, ReadOnly: true,
@ -377,10 +380,20 @@ func (r *VinoReconciler) SetupWithManager(mgr ctrl.Manager) error {
func (r *VinoReconciler) finalize(ctx context.Context, vino *vinov1.Vino) error { func (r *VinoReconciler) finalize(ctx context.Context, vino *vinov1.Vino) error {
// TODO aggregate errors instead // TODO aggregate errors instead
if err := r.Delete(ctx, &appsv1.DaemonSet{ObjectMeta: metav1.ObjectMeta{Name: vino.Name, Namespace: vino.Namespace}}); err != nil { if err := r.Delete(ctx,
&appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: vino.Name, Namespace: vino.Namespace,
},
}); err != nil {
return err return err
} }
if err := r.Delete(ctx, &corev1.ConfigMap{ObjectMeta: metav1.ObjectMeta{Name: vino.Name, Namespace: vino.Namespace}}); err != nil { if err := r.Delete(ctx,
&corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: vino.Name, Namespace: vino.Namespace,
},
}); err != nil {
return err return err
} }
controllerutil.RemoveFinalizer(vino, vinov1.VinoFinalizer) controllerutil.RemoveFinalizer(vino, vinov1.VinoFinalizer)
@ -396,7 +409,7 @@ func defaultDaemonSet(vino *vinov1.Vino) (ds *appsv1.DaemonSet) {
biDirectional := corev1.MountPropagationBidirectional biDirectional := corev1.MountPropagationBidirectional
ds = &appsv1.DaemonSet{ return &appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: vino.Name, Name: vino.Name,
Namespace: vino.Namespace, Namespace: vino.Namespace,
@ -522,7 +535,6 @@ func defaultDaemonSet(vino *vinov1.Vino) (ds *appsv1.DaemonSet) {
}, },
}, },
} }
return
} }
func applyRuntimeObject(ctx context.Context, key client.ObjectKey, obj client.Object, c client.Client) error { func applyRuntimeObject(ctx context.Context, key client.ObjectKey, obj client.Object, c client.Client) error {

View File

@ -15,6 +15,7 @@ limitations under the License.
package main package main
import ( import (
"context"
"encoding/json" "encoding/json"
"net" "net"
"os" "os"
@ -32,7 +33,10 @@ import (
) )
func main() { func main() {
logger, _ := zap.NewProduction() logger, err := zap.NewProduction()
if err != nil {
log.Fatal(err.Error())
}
log := logger.Named("nodelabeler") log := logger.Named("nodelabeler")
nodeName, ok := os.LookupEnv("NODE") nodeName, ok := os.LookupEnv("NODE")
@ -69,14 +73,15 @@ func main() {
labels := map[string]string{ labels := map[string]string{
"airshipit.org/vino.nodebridgegw": ifaceAddr, "airshipit.org/vino.nodebridgegw": ifaceAddr,
} }
ctx := context.Background()
node, err := clientset.CoreV1().Nodes().Get(nodeName, metav1.GetOptions{}) node, err := clientset.CoreV1().Nodes().Get(ctx, nodeName, metav1.GetOptions{})
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())
} }
for label, value := range labels { for label, value := range labels {
err = addLabelToNode(clientset, node, label, value) err = addLabelToNode(ctx, clientset, node, label, value)
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal(err.Error())
} }
@ -86,7 +91,11 @@ func main() {
} }
} }
func addLabelToNode(clientset *kubernetes.Clientset, node *v1.Node, key string, value string) error { func addLabelToNode(
ctx context.Context,
clientset *kubernetes.Clientset,
node *v1.Node,
key, value string) error {
log.Info("Applying node label", log.Info("Applying node label",
zap.String(key, value), zap.String(key, value),
) )
@ -113,7 +122,7 @@ func addLabelToNode(clientset *kubernetes.Clientset, node *v1.Node, key string,
zap.String("patch", string(patch)), zap.String("patch", string(patch)),
) )
_, err = clientset.CoreV1().Nodes().Patch(node.Name, types.MergePatchType, patch) _, err = clientset.CoreV1().Nodes().Patch(ctx, node.Name, types.MergePatchType, patch, metav1.PatchOptions{})
if err != nil { if err != nil {
return err return err
} }