Skip to content

Commit

Permalink
Merge pull request #131 from Fedosin/download_manifests
Browse files Browse the repository at this point in the history
🐛 Download and store manifests from provider URL
  • Loading branch information
k8s-ci-robot authored Jun 6, 2023
2 parents f51695b + 67bed92 commit eeab2cb
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
BootstrapProviderKind = "BootstrapProvider"
BootstrapProviderListKind = "BootstrapProviderList"
)

type BootstrapProviderWrapper struct {
*operatorv1.BootstrapProvider
}
Expand Down Expand Up @@ -59,6 +54,10 @@ func (b *BootstrapProviderWrapper) GetObject() client.Object {
return b.BootstrapProvider
}

func (b *BootstrapProviderWrapper) GetType() string {
return "bootstrap"
}

type BootstrapProviderListWrapper struct {
*operatorv1.BootstrapProviderList
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
ControlPlaneProviderKind = "ControlPlaneProvider"
ControlPlaneProviderListKind = "ControlPlaneProviderList"
)

type ControlPlaneProviderWrapper struct {
*operatorv1.ControlPlaneProvider
}
Expand Down Expand Up @@ -59,6 +54,10 @@ func (c *ControlPlaneProviderWrapper) GetObject() client.Object {
return c.ControlPlaneProvider
}

func (c *ControlPlaneProviderWrapper) GetType() string {
return "controlplane"
}

type ControlPlaneProviderListWrapper struct {
*operatorv1.ControlPlaneProviderList
}
Expand Down
9 changes: 4 additions & 5 deletions internal/controller/genericprovider/coreprovider_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
CoreProviderKind = "CoreProvider"
CoreProviderListKind = "CoreProviderList"
)

type CoreProviderWrapper struct {
*operatorv1.CoreProvider
}
Expand Down Expand Up @@ -59,6 +54,10 @@ func (c *CoreProviderWrapper) GetObject() client.Object {
return c.CoreProvider
}

func (c *CoreProviderWrapper) GetType() string {
return "core"
}

type CoreProviderListWrapper struct {
*operatorv1.CoreProviderList
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type GenericProvider interface {
GetStatus() operatorv1.ProviderStatus
SetStatus(in operatorv1.ProviderStatus)
GetObject() client.Object
GetType() string
}

type GenericProviderList interface {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
InfrastructureProviderKind = "InfrastructureProvider"
InfrastructureProviderListKind = "InfrastructureProviderList"
)

type InfrastructureProviderWrapper struct {
*operatorv1.InfrastructureProvider
}
Expand Down Expand Up @@ -59,6 +54,10 @@ func (i *InfrastructureProviderWrapper) GetObject() client.Object {
return i.InfrastructureProvider
}

func (i *InfrastructureProviderWrapper) GetType() string {
return "infrastructure"
}

type InfrastructureProviderListWrapper struct {
*operatorv1.InfrastructureProviderList
}
Expand Down
2 changes: 2 additions & 0 deletions internal/controller/genericprovider_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ func (r *GenericProviderReconciler) reconcile(ctx context.Context, provider gene
reconciler := newPhaseReconciler(*r, provider, genericProviderList)
phases := []reconcilePhaseFn{
reconciler.preflightChecks,
reconciler.initializePhaseReconciler,
reconciler.downloadManifests,
reconciler.load,
reconciler.fetch,
reconciler.preInstall,
Expand Down
160 changes: 160 additions & 0 deletions internal/controller/manifests_downloader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
Copyright 2023 The Kubernetes Authors.
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 controller

import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha1"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

const (
configMapVersionLabel = "provider.cluster.x-k8s.io/version"
configMapTypeLabel = "provider.cluster.x-k8s.io/type"
configMapNameLabel = "provider.cluster.x-k8s.io/name"
operatorManagedLabel = "managed-by.operator.cluster.x-k8s.io"

metadataConfigMapKey = "metadata"
componentsConfigMapKey = "components"
)

// downloadManifests downloads CAPI manifests from a url.
func (p *phaseReconciler) downloadManifests(ctx context.Context) (reconcile.Result, error) {
log := ctrl.LoggerFrom(ctx)

log.Info("Downloading provider manifests")

// Return immediately if a custom config map is used instead of a url.
if p.provider.GetSpec().FetchConfig != nil && p.provider.GetSpec().FetchConfig.Selector != nil {
log.V(5).Info("Custom config map is used, skip downloading provider manifests")

return reconcile.Result{}, nil
}

// Check if manifests are already downloaded and stored in a configmap
labelSelector := metav1.LabelSelector{
MatchLabels: p.prepareConfigMapLabels(),
}

exists, err := p.checkConfigMapExists(ctx, labelSelector)
if err != nil {
return reconcile.Result{}, wrapPhaseError(err, "failed to check that config map with manifests exists", operatorv1.PreflightCheckCondition)
}

if exists {
log.V(5).Info("Config map with downloaded manifests already exists, skip downloading provider manifests")

return reconcile.Result{}, nil
}

repo, err := repositoryFactory(p.providerConfig, p.configClient.Variables())
if err != nil {
err = fmt.Errorf("failed to create repo from provider url for provider %q: %w", p.provider.GetName(), err)

return reconcile.Result{}, wrapPhaseError(err, operatorv1.ComponentsFetchErrorReason, operatorv1.PreflightCheckCondition)
}

// Fetch the provider metadata and components yaml files from the provided repository GitHub/GitLab.
metadataFile, err := repo.GetFile(p.options.Version, metadataFile)
if err != nil {
err = fmt.Errorf("failed to read %q from the repository for provider %q: %w", metadataFile, p.provider.GetName(), err)

return reconcile.Result{}, wrapPhaseError(err, operatorv1.ComponentsFetchErrorReason, operatorv1.PreflightCheckCondition)
}

componentsFile, err := repo.GetFile(p.options.Version, repo.ComponentsPath())
if err != nil {
err = fmt.Errorf("failed to read %q from the repository for provider %q: %w", componentsFile, p.provider.GetName(), err)

return reconcile.Result{}, wrapPhaseError(err, operatorv1.ComponentsFetchErrorReason, operatorv1.PreflightCheckCondition)
}

if err := p.createManifestsConfigMap(ctx, string(metadataFile), string(componentsFile)); err != nil {
err = fmt.Errorf("failed to create config map for provider %q: %w", p.provider.GetName(), err)

return reconcile.Result{}, wrapPhaseError(err, operatorv1.ComponentsFetchErrorReason, operatorv1.PreflightCheckCondition)
}

return reconcile.Result{}, nil
}

// checkConfigMapExists checks if a config map exists in Kubernetes with the given LabelSelector.
func (p *phaseReconciler) checkConfigMapExists(ctx context.Context, labelSelector metav1.LabelSelector) (bool, error) {
labelSet := labels.Set(labelSelector.MatchLabels)
listOpts := []client.ListOption{
client.MatchingLabelsSelector{Selector: labels.SelectorFromSet(labelSet)},
}

var configMapList corev1.ConfigMapList

if err := p.ctrlClient.List(ctx, &configMapList, listOpts...); err != nil {
return false, fmt.Errorf("failed to list ConfigMaps: %w", err)
}

if len(configMapList.Items) > 1 {
return false, fmt.Errorf("more than one config maps were found for given selector: %v", labelSelector.String())
}

return len(configMapList.Items) == 1, nil
}

// prepareConfigMapLabels returns labels that identify a config map with downloaded manifests.
func (p *phaseReconciler) prepareConfigMapLabels() map[string]string {
return map[string]string{
configMapVersionLabel: p.provider.GetSpec().Version,
configMapTypeLabel: p.provider.GetType(),
configMapNameLabel: p.provider.GetName(),
operatorManagedLabel: "true",
}
}

// createManifestsConfigMap creates a config map with downloaded manifests.
func (p *phaseReconciler) createManifestsConfigMap(ctx context.Context, metadata, components string) error {
configMapName := fmt.Sprintf("%s-%s-%s", p.provider.GetType(), p.provider.GetName(), p.provider.GetSpec().Version)

configMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: configMapName,
Namespace: p.provider.GetNamespace(),
Labels: p.prepareConfigMapLabels(),
},
Data: map[string]string{
metadataConfigMapKey: metadata,
componentsConfigMapKey: components,
},
}

gvk := p.provider.GetObjectKind().GroupVersionKind()

configMap.SetOwnerReferences([]metav1.OwnerReference{
{
APIVersion: gvk.GroupVersion().String(),
Kind: gvk.Kind,
Name: p.provider.GetName(),
UID: p.provider.GetUID(),
},
})

return p.ctrlClient.Create(ctx, configMap)
}
72 changes: 72 additions & 0 deletions internal/controller/manifests_downloader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
Copyright 2022 The Kubernetes Authors.
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 controller

import (
"context"
"testing"

. "github.com/onsi/gomega"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

operatorv1 "sigs.k8s.io/cluster-api-operator/api/v1alpha1"
"sigs.k8s.io/cluster-api-operator/internal/controller/genericprovider"
)

func TestManifestsDownloader(t *testing.T) {
g := NewWithT(t)

ctx := context.Background()

fakeclient := fake.NewClientBuilder().WithObjects().Build()

namespace := "test-namespace"

p := &phaseReconciler{
ctrlClient: fakeclient,
provider: &genericprovider.CoreProviderWrapper{
CoreProvider: &operatorv1.CoreProvider{
ObjectMeta: metav1.ObjectMeta{
Name: "cluster-api",
Namespace: namespace,
},
Spec: operatorv1.CoreProviderSpec{
ProviderSpec: operatorv1.ProviderSpec{
Version: "v1.4.2",
},
},
},
},
}

_, err := p.initializePhaseReconciler(ctx)
g.Expect(err).ToNot(HaveOccurred())

_, err = p.downloadManifests(ctx)
g.Expect(err).ToNot(HaveOccurred())

// Ensure that config map was created
labelSelector := metav1.LabelSelector{
MatchLabels: p.prepareConfigMapLabels(),
}

exists, err := p.checkConfigMapExists(ctx, labelSelector)
g.Expect(err).ToNot(HaveOccurred())

g.Expect(exists).To(BeTrue())
}
Loading

0 comments on commit eeab2cb

Please sign in to comment.