forked from Mellanox/network-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate_cni_plugins.go
171 lines (151 loc) · 6.13 KB
/
state_cni_plugins.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
Copyright 2020 NVIDIA
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package state
import (
"context"
"github.com/go-logr/logr"
"github.com/pkg/errors"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/log"
mellanoxv1alpha1 "github.com/Mellanox/network-operator/api/v1alpha1"
"github.com/Mellanox/network-operator/pkg/config"
"github.com/Mellanox/network-operator/pkg/consts"
"github.com/Mellanox/network-operator/pkg/render"
"github.com/Mellanox/network-operator/pkg/utils"
)
const stateCNIPluginsName = "state-container-networking-plugins"
const stateCNIPluginsDescription = "Container Networking CNI Plugins deployed in the cluster"
// NewStateCNIPlugins creates a new state for secondary container networking CNI plugins
func NewStateCNIPlugins(
k8sAPIClient client.Client, scheme *runtime.Scheme, manifestDir string) (State, ManifestRenderer, error) {
files, err := utils.GetFilesWithSuffix(manifestDir, render.ManifestFileSuffix...)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to get files from manifest dir")
}
renderer := render.NewRenderer(files)
state := &stateCNIPlugins{
stateSkel: stateSkel{
name: stateCNIPluginsName,
description: stateCNIPluginsDescription,
client: k8sAPIClient,
scheme: scheme,
renderer: renderer,
}}
return state, state, nil
}
type stateCNIPlugins struct {
stateSkel
}
// CNIPluginsManifestRenderData contains information used to render Kubernetes objects related to CNIPlugins.
type CNIPluginsManifestRenderData struct {
CrSpec *mellanoxv1alpha1.ImageSpec
Tolerations []v1.Toleration
NodeAffinity *v1.NodeAffinity
RuntimeSpec *cniRuntimeSpec
}
// Sync attempt to get the system to match the desired state which State represent.
// a sync operation must be relatively short and must not block the execution thread.
//
//nolint:dupl
func (s *stateCNIPlugins) Sync(
ctx context.Context, customResource interface{}, infoCatalog InfoCatalog) (SyncState, error) {
reqLogger := log.FromContext(ctx)
cr := customResource.(*mellanoxv1alpha1.NicClusterPolicy)
reqLogger.V(consts.LogLevelInfo).Info(
"Sync Custom resource", "State:", s.name, "Name:", cr.Name, "Namespace:", cr.Namespace)
if cr.Spec.SecondaryNetwork == nil || cr.Spec.SecondaryNetwork.CniPlugins == nil {
// Either this state was not required to run or an update occurred and we need to remove
// the resources that where created.
return s.handleStateObjectsDeletion(ctx)
}
// Fill ManifestRenderData and render objects
staticInfo := infoCatalog.GetStaticConfigProvider()
if staticInfo == nil {
return SyncStateError, errors.New("unexpected state, catalog does not provide static info")
}
objs, err := s.GetManifestObjects(ctx, cr, infoCatalog, reqLogger)
if err != nil {
return SyncStateNotReady, errors.Wrap(err, "failed to create k8s objects from manifest")
}
if len(objs) == 0 {
return SyncStateNotReady, nil
}
// Create objects if they dont exist, Update objects if they do exist
err = s.createOrUpdateObjs(ctx, func(obj *unstructured.Unstructured) error {
if err := controllerutil.SetControllerReference(cr, obj, s.scheme); err != nil {
return errors.Wrap(err, "failed to set controller reference for object")
}
return nil
}, objs)
if err != nil {
return SyncStateNotReady, errors.Wrap(err, "failed to create/update objects")
}
waitForStaleObjectsRemoval, err := s.handleStaleStateObjects(ctx, objs)
if err != nil {
return SyncStateNotReady, errors.Wrap(err, "failed to handle state stale objects")
}
if waitForStaleObjectsRemoval {
return SyncStateNotReady, nil
}
// Check objects status
syncState, err := s.getSyncState(ctx, objs)
if err != nil {
return SyncStateNotReady, errors.Wrap(err, "failed to get sync state")
}
return syncState, nil
}
// Get a map of source kinds that should be watched for the state keyed by the source kind name
func (s *stateCNIPlugins) GetWatchSources() map[string]client.Object {
wr := make(map[string]client.Object)
wr["DaemonSet"] = &appsv1.DaemonSet{}
return wr
}
//nolint:dupl
func (s *stateCNIPlugins) GetManifestObjects(
_ context.Context, cr *mellanoxv1alpha1.NicClusterPolicy,
catalog InfoCatalog, reqLogger logr.Logger) ([]*unstructured.Unstructured, error) {
if cr == nil || cr.Spec.SecondaryNetwork == nil || cr.Spec.SecondaryNetwork.CniPlugins == nil {
return nil, errors.New("failed to render objects: state spec is nil")
}
staticConfig := catalog.GetStaticConfigProvider()
if staticConfig == nil {
return nil, errors.New("staticConfig provider required")
}
clusterInfo := catalog.GetClusterTypeProvider()
if clusterInfo == nil {
return nil, errors.New("clusterInfo provider required")
}
renderData := &CNIPluginsManifestRenderData{
CrSpec: cr.Spec.SecondaryNetwork.CniPlugins,
Tolerations: cr.Spec.Tolerations,
NodeAffinity: cr.Spec.NodeAffinity,
RuntimeSpec: &cniRuntimeSpec{
runtimeSpec: runtimeSpec{config.FromEnv().State.NetworkOperatorResourceNamespace},
CniBinDirectory: utils.GetCniBinDirectory(staticConfig, clusterInfo),
ContainerResources: createContainerResourcesMap(cr.Spec.SecondaryNetwork.CniPlugins.ContainerResources),
},
}
// render objects
reqLogger.V(consts.LogLevelDebug).Info("Rendering objects", "data:", renderData)
objs, err := s.renderer.RenderObjects(&render.TemplatingData{Data: renderData})
if err != nil {
return nil, errors.Wrap(err, "failed to render objects")
}
reqLogger.V(consts.LogLevelDebug).Info("Rendered", "objects:", objs)
return objs, nil
}