Skip to content

Commit

Permalink
Collector deployment was replaced with daemonSet
Browse files Browse the repository at this point in the history
Signed-off-by: Tigran Manasyan <tigran.manasyan@xored.com>
  • Loading branch information
Tigran Manasyan committed Nov 3, 2020
1 parent f86fb3c commit 5689cc0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 62 deletions.
12 changes: 9 additions & 3 deletions deployments/prefixes-collector/collector.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
apiVersion: apps/v1
kind: Deployment
kind: DaemonSet
metadata:
name: excluded-prefixes-collector
namespace: excluded-prefixes-collector
Expand All @@ -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
8 changes: 0 additions & 8 deletions deployments/prefixes-collector/config-map.yaml

This file was deleted.

26 changes: 26 additions & 0 deletions k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
62 changes: 11 additions & 51 deletions test/excluded_prefixes_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -76,15 +73,14 @@ 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()
}

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")
}
Expand Down Expand Up @@ -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, ","),
Expand All @@ -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, ","),
Expand All @@ -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, ","),
Expand All @@ -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) {
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 5689cc0

Please sign in to comment.