diff --git a/deployments/prefixes-collector/collector.yaml b/deployments/prefixes-collector/collector.yaml index 61070a1f2..e65cbe080 100644 --- a/deployments/prefixes-collector/collector.yaml +++ b/deployments/prefixes-collector/collector.yaml @@ -1,6 +1,6 @@ --- apiVersion: apps/v1 -kind: Deployment +kind: DaemonSet metadata: name: excluded-prefixes-collector namespace: excluded-prefixes-collector @@ -21,9 +21,15 @@ spec: imagePullPolicy: IfNotPresent name: excluded-prefixes-collector env: - - name: EXCLUDE_PREFIXES_K8S_NSM_CONFIG_MAP_NAME - value: nsm-config - name: EXCLUDE_PREFIXES_K8S_CONFIG_MAP_NAMESPACE value: default - name: EXCLUDE_PREFIXES_K8S_CONFIG_MAP_NAME value: excluded-prefixes-config + volumeMounts: + - name: config-dir + mountPath: /var/lib/networkservicemesh/config/ + volumes: + - name: config-dir + hostPath: + path: /var/lib/networkservicemesh/config/ + type: DirectoryOrCreate diff --git a/deployments/prefixes-collector/config-map.yaml b/deployments/prefixes-collector/config-map.yaml deleted file mode 100644 index 76053d9e5..000000000 --- a/deployments/prefixes-collector/config-map.yaml +++ /dev/null @@ -1,8 +0,0 @@ ---- -apiVersion: v1 -kind: ConfigMap -metadata: - name: nsm-config - namespace: excluded-prefixes-collector -data: - excluded_prefixes.yaml: "Prefixes:\n" diff --git a/k8s/k8s.go b/k8s/k8s.go index c599080be..db281cf28 100644 --- a/k8s/k8s.go +++ b/k8s/k8s.go @@ -83,6 +83,32 @@ func ApplyDeployment(path string, mutators ...func(deployment *apiv1.Deployment) return err } +// ApplyDaemonSet is analogy of 'kubeclt apply -f path' but with mutating daemonSet before apply +func ApplyDaemonSet(path string, mutators ...func(deployment *apiv1.DaemonSet)) error { + client, err := Client() + if err != nil { + return err + } + b, err := ioutil.ReadFile(filepath.Clean(path)) + if err != nil { + return err + } + var d apiv1.DaemonSet + if parseErr := yaml.Unmarshal(b, &d); parseErr != nil { + return parseErr + } + for _, m := range mutators { + m(&d) + } + + if d.Namespace == "" { + d.Namespace = namespace + } + + _, err = client.AppsV1().DaemonSets(d.Namespace).Create(&d) + return err +} + // ShowLogs prints logs into console all containers of pods func ShowLogs(namespace string, options ...*exechelper.Option) { client, err := Client() diff --git a/test/excluded_prefixes_collector_test.go b/test/excluded_prefixes_collector_test.go index 448ae0adf..00af3eb8e 100644 --- a/test/excluded_prefixes_collector_test.go +++ b/test/excluded_prefixes_collector_test.go @@ -38,11 +38,8 @@ const ( prefixesFilePath = "/var/lib/networkservicemesh/config/excluded_prefixes.yaml" collectorNamespace = "excluded-prefixes-collector" excludedPrefixesEnv = "EXCLUDE_PREFIXES_K8S_EXCLUDED_PREFIXES" - excludedPrefixesKey = "excluded_prefixes.yaml" - // why defaultTimeout is 120 sec: - // https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#mounted-configmaps-are-updated-automatically - defaultTimeout = time.Second * 120 - defaultTick = time.Second * 5 + defaultTimeout = time.Second * 30 + defaultTick = time.Second * 1 ) type ExcludedPrefixesSuite struct { @@ -76,7 +73,6 @@ func (et *ExcludedPrefixesSuite) SetupSuite() { et.Require().NoError(exechelper.Run("kubectl apply -f ../deployments/prefixes-collector/collector-namespace.yaml", et.options...)) et.Require().NoError(exechelper.Run("kubectl apply -f ../deployments/prefixes-collector/collector-account.yaml", et.options...)) et.Require().NoError(exechelper.Run("kubectl apply -f ../deployments/prefixes-collector/collector-cluster-role.yaml", et.options...)) - et.Require().NoError(exechelper.Run("kubectl apply -f ../deployments/prefixes-collector/config-map.yaml", et.options...)) et.setupAlpine() } @@ -84,7 +80,7 @@ func (et *ExcludedPrefixesSuite) SetupSuite() { func (et *ExcludedPrefixesSuite) TearDownTest() { k8s.ShowLogs(collectorNamespace, et.options...) - et.Require().NoError(exechelper.Run("kubectl delete deployment -n excluded-prefixes-collector excluded-prefixes-collector", et.options...)) + et.Require().NoError(exechelper.Run("kubectl delete daemonset -n excluded-prefixes-collector excluded-prefixes-collector", et.options...)) et.Require().NoError(exechelper.Run("kubectl delete pods -n excluded-prefixes-collector -l app=excluded-prefixes-collector --now", et.options...)) logrus.Info("Collector deleted") } @@ -123,7 +119,7 @@ func (et *ExcludedPrefixesSuite) TestWithAllPrefixes() { "134.65.0.0/16", } - et.Require().NoError(k8s.ApplyDeployment("../deployments/prefixes-collector/collector.yaml", func(collector *v1.Deployment) { + et.Require().NoError(k8s.ApplyDaemonSet("../deployments/prefixes-collector/collector.yaml", func(collector *v1.DaemonSet) { collector.Spec.Template.Spec.Containers[0].Env = append(collector.Spec.Template.Spec.Containers[0].Env, corev1.EnvVar{ Name: excludedPrefixesEnv, Value: strings.Join(envPrefixes, ","), @@ -145,7 +141,7 @@ func (et *ExcludedPrefixesSuite) TestWithCorrectEnvPrefixes() { "134.65.0.0/16", } - et.Require().NoError(k8s.ApplyDeployment("../deployments/prefixes-collector/collector.yaml", func(collector *v1.Deployment) { + et.Require().NoError(k8s.ApplyDaemonSet("../deployments/prefixes-collector/collector.yaml", func(collector *v1.DaemonSet) { collector.Spec.Template.Spec.Containers[0].Env = append(collector.Spec.Template.Spec.Containers[0].Env, corev1.EnvVar{ Name: excludedPrefixesEnv, Value: strings.Join(envPrefixes, ","), @@ -161,7 +157,7 @@ func (et *ExcludedPrefixesSuite) TestWithIncorrectEnvPrefixes() { "306.306.306.0", } - et.Require().NoError(k8s.ApplyDeployment("../deployments/prefixes-collector/collector.yaml", func(collector *v1.Deployment) { + et.Require().NoError(k8s.ApplyDaemonSet("../deployments/prefixes-collector/collector.yaml", func(collector *v1.DaemonSet) { collector.Spec.Template.Spec.Containers[0].Env = append(collector.Spec.Template.Spec.Containers[0].Env, corev1.EnvVar{ Name: excludedPrefixesEnv, Value: strings.Join(envPrefixes, ","), @@ -170,31 +166,11 @@ func (et *ExcludedPrefixesSuite) TestWithIncorrectEnvPrefixes() { client, err := k8s.Client() et.Require().NoError(err) - inter := client.AppsV1().Deployments(collectorNamespace) + inter := client.AppsV1().DaemonSets(collectorNamespace) et.Eventually(func() bool { - deployment, err := inter.Get("excluded-prefixes-collector", metav1.GetOptions{}) - return err == nil && deployment.Status.AvailableReplicas == 0 - }, time.Second*30, time.Second) -} - -func (et *ExcludedPrefixesSuite) TestCatchNsmConfigExternalChange() { - et.Require().NoError(exechelper.Run("kubectl apply -f ../deployments/prefixes-collector/collector.yaml", et.options...)) - et.Eventually(et.checkPrefixes(kubeAdmPrefixes), defaultTimeout, defaultTick) - - et.Require().NoError(exechelper.Run("kubectl replace -f ../deployments/prefixes-collector/config-map.yaml", et.options...)) - - expectedPrefixesYaml, err := yaml.Marshal(&prefixes{kubeAdmPrefixes}) - et.Require().NoError(err) - - client, err := k8s.Client() - et.Require().NoError(err) - configMapInterface := client.CoreV1().ConfigMaps(collectorNamespace) - - et.Eventually(func() bool { - configMap, err := configMapInterface.Get("nsm-config", metav1.GetOptions{}) - et.Require().NoError(err) - return configMap.Data[excludedPrefixesKey] == string(expectedPrefixesYaml) - }, time.Second*15, time.Second) + daemonSet, err := inter.Get("excluded-prefixes-collector", metav1.GetOptions{}) + return err == nil && daemonSet.Status.NumberAvailable == 0 + }, defaultTimeout, defaultTick) } func TestExcludedPrefixesSuite(t *testing.T) { @@ -204,22 +180,6 @@ func TestExcludedPrefixesSuite(t *testing.T) { func (et *ExcludedPrefixesSuite) setupAlpine() { et.Require().NoError(k8s.ApplyDeployment("../deployments/alpine.yaml", func(alpine *v1.Deployment) { alpine.Namespace = collectorNamespace - alpine.Spec.Template.Spec.Volumes = append(alpine.Spec.Template.Spec.Volumes, corev1.Volume{ - Name: "config-volume", - VolumeSource: corev1.VolumeSource{ - ConfigMap: &corev1.ConfigMapVolumeSource{ - LocalObjectReference: corev1.LocalObjectReference{ - Name: "nsm-config", - }, - }, - }, - }) - - alpine.Spec.Template.Spec.Containers[0].VolumeMounts = append(alpine.Spec.Template.Spec.Containers[0].VolumeMounts, - corev1.VolumeMount{ - Name: "config-volume", - MountPath: "/var/lib/networkservicemesh/config", - }) })) var podInfo *corev1.Pod @@ -247,7 +207,7 @@ func (et *ExcludedPrefixesSuite) checkPrefixes(expectedPrefixes []string) func() var sb strings.Builder cmd := fmt.Sprintf("kubectl exec -n excluded-prefixes-collector -ti %v -- cat %v", et.alpinePodName, prefixesFilePath) err := exechelper.Run(cmd, exechelper.WithStdout(&sb)) - logrus.Infof("Actual: %v Expected: %v", sb.String(), string(expectedPrefixesYaml)) + logrus.Infof("Current: %v Expected: %v", sb.String(), string(expectedPrefixesYaml)) return err == nil && sb.String() == string(expectedPrefixesYaml) } }