Skip to content
This repository has been archived by the owner on Apr 7, 2020. It is now read-only.

Commit

Permalink
Ensure Cloud provider config content as part of kubelet configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Svetlina Shopova committed Jun 18, 2019
1 parent 946f575 commit 051d7d4
Show file tree
Hide file tree
Showing 10 changed files with 264 additions and 10 deletions.
2 changes: 2 additions & 0 deletions controllers/provider-azure/pkg/azure/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const (

// CloudProviderConfigName is the name of the configmap containing the cloud provider config.
CloudProviderConfigName = "cloud-provider-config"
// CloudProviderConfigMapKey is the key storing the cloud provider config as value in the cloud provider configmap.
CloudProviderConfigMapKey = "cloudprovider.conf"
// BackupSecretName is the name of the secret containing the credentials for storing the backups of Shoot clusters.
BackupSecretName = "etcd-backup"
)
Expand Down
26 changes: 26 additions & 0 deletions controllers/provider-azure/pkg/webhook/controlplane/ensurer.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ package controlplane

import (
"context"
"github.com/pkg/errors"

"github.com/gardener/gardener-extensions/controllers/provider-azure/pkg/azure"
"github.com/gardener/gardener-extensions/pkg/webhook/controlplane"
"github.com/gardener/gardener-extensions/pkg/webhook/controlplane/genericmutator"

"github.com/coreos/go-systemd/unit"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -143,3 +145,27 @@ func (e *ensurer) EnsureKubeletConfiguration(ctx context.Context, kubeletConfig
delete(kubeletConfig.FeatureGates, "CSIDriverRegistry")
return nil
}

//ShouldProvisionKubeletCloudProviderConfig returns if the cloudprovider.config file should be added to the kubelet configuration.
func (e *ensurer) ShouldProvisionKubeletCloudProviderConfig() bool {
return true
}

//EnsureKubeletCloudProviderConfig ensures that the cloudprovider.config file conforms to the provider requirements.
func (e *ensurer) EnsureKubeletCloudProviderConfig(ctx context.Context, data *string, namespace string) error {
// Get `cloud-provider-config` ConfigMap
var cm corev1.ConfigMap
err := e.client.Get(ctx, kutil.Key(namespace, azure.CloudProviderConfigName), &cm)
if err != nil {
return errors.Wrapf(err, "could not get configmap with name '%s' and namespace '%s'", azure.CloudProviderConfigName, namespace)
}

// Check if the data has "cloudprovider.conf" key
if cm.Data == nil || cm.Data[azure.CloudProviderConfigMapKey] == "" {
return nil
}

// Overwrite data variable
*data = cm.Data[azure.CloudProviderConfigMapKey]
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package controlplane

import (
"context"
"github.com/gardener/gardener-extensions/pkg/util"
"testing"

"github.com/gardener/gardener-extensions/controllers/provider-azure/pkg/azure"
Expand All @@ -38,7 +39,8 @@ import (
)

const (
namespace = "test"
namespace = "test"
cloudProviderConfigContent = "[Global]\nauth-url: https://cluster.eu-de-200.cloud.sap:5000/v3/\n"
)

func TestController(t *testing.T) {
Expand All @@ -53,11 +55,11 @@ var _ = Describe("Ensurer", func() {
cmKey = client.ObjectKey{Namespace: namespace, Name: azure.CloudProviderConfigName}
cm = &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: azure.CloudProviderConfigName},
Data: map[string]string{"abc": "xyz"},
Data: map[string]string{"abc": "xyz", azure.CloudProviderConfigMapKey: cloudProviderConfigContent},
}

annotations = map[string]string{
"checksum/configmap-" + azure.CloudProviderConfigName: "08a7bc7fe8f59b055f173145e211760a83f02cf89635cef26ebb351378635606",
"checksum/configmap-" + azure.CloudProviderConfigName: "2ac8b96caad089f7b0217f0b2916ff4e8d4346655746de55178207e180cf0bbe",
}
)

Expand Down Expand Up @@ -287,6 +289,43 @@ var _ = Describe("Ensurer", func() {
Expect(&kubeletConfig).To(Equal(newKubeletConfig))
})
})

Describe("#EnsureKubeletCloudProviderConfig", func() {
var (
existingData = util.StringPtr("[LoadBalancer]\nlb-version=v2\nlb-provider:\n")
emptydata = util.StringPtr("")
)
It("should create element containing cloud provider config content", func() {
// Create mock client
client := mockclient.NewMockClient(ctrl)
client.EXPECT().Get(context.TODO(), cmKey, &corev1.ConfigMap{}).DoAndReturn(clientGet(cm))

// Create ensurer
ensurer := NewEnsurer(logger)
err := ensurer.(inject.Client).InjectClient(client)
Expect(err).NotTo(HaveOccurred())

// Call EnsureKubeletConfiguration method and check the result
err = ensurer.EnsureKubeletCloudProviderConfig(context.TODO(), emptydata, namespace)
Expect(err).To(Not(HaveOccurred()))
Expect(*emptydata).To(Equal(cloudProviderConfigContent))
})
It("should modify existing element containing cloud provider config content", func() {
// Create mock client
client := mockclient.NewMockClient(ctrl)
client.EXPECT().Get(context.TODO(), cmKey, &corev1.ConfigMap{}).DoAndReturn(clientGet(cm))

// Create ensurer
ensurer := NewEnsurer(logger)
err := ensurer.(inject.Client).InjectClient(client)
Expect(err).NotTo(HaveOccurred())

// Call EnsureKubeletConfiguration method and check the result
err = ensurer.EnsureKubeletCloudProviderConfig(context.TODO(), existingData, namespace)
Expect(err).To(Not(HaveOccurred()))
Expect(*existingData).To(Equal(cloudProviderConfigContent))
})
})
})

func checkKubeAPIServerDeployment(dep *appsv1.Deployment, annotations map[string]string) {
Expand Down
2 changes: 2 additions & 0 deletions controllers/provider-openstack/pkg/openstack/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ const (

// CloudProviderConfigName is the name of the configmap containing the cloud provider config.
CloudProviderConfigName = "cloud-provider-config"
// CloudProviderConfigMapKey is the key storing the cloud provider config as value in the cloud provider configmap.
CloudProviderConfigMapKey = "cloudprovider.conf"
// MachineControllerManagerName is a constant for the name of the machine-controller-manager.
MachineControllerManagerName = "machine-controller-manager"
// BackupSecretName defines the name of the secret containing the credentials which are required to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ package controlplane

import (
"context"

"github.com/gardener/gardener-extensions/controllers/provider-openstack/pkg/openstack"
"github.com/gardener/gardener-extensions/pkg/webhook/controlplane"
"github.com/gardener/gardener-extensions/pkg/webhook/controlplane/genericmutator"
"github.com/pkg/errors"

"github.com/coreos/go-systemd/unit"
kutil "github.com/gardener/gardener/pkg/utils/kubernetes"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -126,11 +127,18 @@ func (e *ensurer) EnsureKubeletServiceUnitOptions(ctx context.Context, opts []*u
command = ensureKubeletCommandLineArgs(command)
opt.Value = controlplane.SerializeCommandLine(command, 1, " \\\n ")
}

opts = controlplane.EnsureUnitOption(opts, &unit.UnitOption{
Section: "Service",
Name: "ExecStartPre",
Value: `/bin/sh -c 'hostnamectl set-hostname $(cat /etc/hostname | cut -d '.' -f 1)'`,
})
return opts, nil
}

func ensureKubeletCommandLineArgs(command []string) []string {
command = controlplane.EnsureStringWithPrefix(command, "--cloud-provider=", "openstack")
command = controlplane.EnsureStringWithPrefix(command, "--cloud-config=", "/var/lib/kubelet/cloudprovider.conf")
return command
}

Expand All @@ -143,3 +151,27 @@ func (e *ensurer) EnsureKubeletConfiguration(ctx context.Context, kubeletConfig
delete(kubeletConfig.FeatureGates, "CSIDriverRegistry")
return nil
}

//ShouldProvisionKubeletCloudProviderConfig returns if the cloudprovider.config file should be added to the kubelet configuration.
func (e *ensurer) ShouldProvisionKubeletCloudProviderConfig() bool {
return true
}

//EnsureKubeletCloudProviderConfig ensures that the cloudprovider.config file conforms to the provider requirements.
func (e *ensurer) EnsureKubeletCloudProviderConfig(ctx context.Context, data *string, namespace string) error {
// Get `cloud-provider-config` ConfigMap
var cm corev1.ConfigMap
err := e.client.Get(ctx, kutil.Key(namespace, openstack.CloudProviderConfigName), &cm)
if err != nil {
return errors.Wrapf(err, "could not get configmap with name '%s' and namespace '%s'", openstack.CloudProviderConfigName, namespace)
}

// Check if the data has "cloudprovider.conf" key
if cm.Data == nil || cm.Data[openstack.CloudProviderConfigMapKey] == "" {
return nil
}

// Overwrite data variable
*data = cm.Data[openstack.CloudProviderConfigMapKey]
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package controlplane

import (
"context"
"github.com/gardener/gardener-extensions/pkg/util"
"testing"

"github.com/gardener/gardener-extensions/controllers/provider-openstack/pkg/openstack"
Expand All @@ -38,7 +39,8 @@ import (
)

const (
namespace = "test"
namespace = "test"
cloudProviderConfigContent = "[Global]\nauth-url: https://cluster.eu-de-200.cloud.sap:5000/v3/\n"
)

func TestController(t *testing.T) {
Expand All @@ -53,11 +55,11 @@ var _ = Describe("Ensurer", func() {
cmKey = client.ObjectKey{Namespace: namespace, Name: openstack.CloudProviderConfigName}
cm = &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{Namespace: namespace, Name: openstack.CloudProviderConfigName},
Data: map[string]string{"abc": "xyz"},
Data: map[string]string{"abc": "xyz", openstack.CloudProviderConfigMapKey: cloudProviderConfigContent},
}

annotations = map[string]string{
"checksum/configmap-" + openstack.CloudProviderConfigName: "08a7bc7fe8f59b055f173145e211760a83f02cf89635cef26ebb351378635606",
"checksum/configmap-" + openstack.CloudProviderConfigName: "2ac8b96caad089f7b0217f0b2916ff4e8d4346655746de55178207e180cf0bbe",
}
)

Expand Down Expand Up @@ -245,7 +247,13 @@ var _ = Describe("Ensurer", func() {
Name: "ExecStart",
Value: `/opt/bin/hyperkube kubelet \
--config=/var/lib/kubelet/config/kubelet \
--cloud-provider=openstack`,
--cloud-provider=openstack \
--cloud-config=/var/lib/kubelet/cloudprovider.conf`,
},
{
Section: "Service",
Name: "ExecStartPre",
Value: `/bin/sh -c 'hostnamectl set-hostname $(cat /etc/hostname | cut -d '.' -f 1)'`,
},
}
)
Expand Down Expand Up @@ -287,6 +295,43 @@ var _ = Describe("Ensurer", func() {
Expect(&kubeletConfig).To(Equal(newKubeletConfig))
})
})

Describe("#EnsureKubeletCloudProviderConfig", func() {
var (
existingData = util.StringPtr("[LoadBalancer]\nlb-version=v2\nlb-provider:\n")
emptydata = util.StringPtr("")
)
It("should create element containing cloud provider config content", func() {
// Create mock client
client := mockclient.NewMockClient(ctrl)
client.EXPECT().Get(context.TODO(), cmKey, &corev1.ConfigMap{}).DoAndReturn(clientGet(cm))

// Create ensurer
ensurer := NewEnsurer(logger)
err := ensurer.(inject.Client).InjectClient(client)
Expect(err).NotTo(HaveOccurred())

// Call EnsureKubeletConfiguration method and check the result
err = ensurer.EnsureKubeletCloudProviderConfig(context.TODO(), emptydata, namespace)
Expect(err).To(Not(HaveOccurred()))
Expect(*emptydata).To(Equal(cloudProviderConfigContent))
})
It("should modify existing element containing cloud provider config content", func() {
// Create mock client
client := mockclient.NewMockClient(ctrl)
client.EXPECT().Get(context.TODO(), cmKey, &corev1.ConfigMap{}).DoAndReturn(clientGet(cm))

// Create ensurer
ensurer := NewEnsurer(logger)
err := ensurer.(inject.Client).InjectClient(client)
Expect(err).NotTo(HaveOccurred())

// Call EnsureKubeletConfiguration method and check the result
err = ensurer.EnsureKubeletCloudProviderConfig(context.TODO(), existingData, namespace)
Expect(err).To(Not(HaveOccurred()))
Expect(*existingData).To(Equal(cloudProviderConfigContent))
})
})
})

func checkKubeAPIServerDeployment(dep *appsv1.Deployment, annotations map[string]string) {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 051d7d4

Please sign in to comment.