Skip to content

Commit

Permalink
Load provider definition dynamically (#944)
Browse files Browse the repository at this point in the history
* Load provider definition dynamically

* Fix `adopted` cluster deployment
  • Loading branch information
s3rj1k authored Jan 28, 2025
1 parent cae713c commit 5470743
Show file tree
Hide file tree
Showing 19 changed files with 304 additions and 127 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ vendor

# mkdocs folder
mkdocs

# Loadable providers
templates/provider/kcm/files/
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ RUN go mod download
COPY cmd/main.go cmd/main.go
COPY api/ api/
COPY internal/ internal/
COPY providers/ providers/

# Build
# the GOARCH has not a default value to allow the binary be built according to the host where the command
Expand Down
15 changes: 12 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,13 @@ $(IMAGES_PACKAGE_DIR): | $(LOCALBIN)

TEMPLATE_FOLDERS = $(patsubst $(TEMPLATES_DIR)/%,%,$(wildcard $(TEMPLATES_DIR)/*))

.PHONY: load-providers
load-providers:
@mkdir -p $(PROVIDER_TEMPLATES_DIR)/kcm/files/providers
@cp -a providers/*.yml $(PROVIDER_TEMPLATES_DIR)/kcm/files/providers/

.PHONY: helm-package
helm-package: $(CHARTS_PACKAGE_DIR) $(EXTENSION_CHARTS_PACKAGE_DIR) helm
helm-package: $(CHARTS_PACKAGE_DIR) $(EXTENSION_CHARTS_PACKAGE_DIR) helm load-providers
@make $(patsubst %,package-%-tmpl,$(TEMPLATE_FOLDERS))

package-%-tmpl:
Expand Down Expand Up @@ -226,7 +231,11 @@ endif
.PHONY: kind-deploy
kind-deploy: kind
@if ! $(KIND) get clusters | grep -q "^$(KIND_CLUSTER_NAME)$$"; then \
$(KIND) create cluster -n $(KIND_CLUSTER_NAME); \
if [ -n "$(KIND_CONFIG_PATH)" ]; then \
$(KIND) create cluster -n $(KIND_CLUSTER_NAME) --config "$(KIND_CONFIG_PATH)"; \
else \
$(KIND) create cluster -n $(KIND_CLUSTER_NAME); \
fi \
fi

.PHONY: kind-undeploy
Expand Down Expand Up @@ -326,7 +335,7 @@ dev-release:

.PHONY: dev-adopted-creds
dev-adopted-creds: envsubst
@NAMESPACE=$(NAMESPACE) $(ENVSUBST) -i config/dev/adopted-credentials.yaml | $(KUBECTL) apply -f -
@NAMESPACE=$(NAMESPACE) $(ENVSUBST) -no-unset -i config/dev/adopted-credentials.yaml | $(KUBECTL) apply -f -

.PHONY: dev-aws-creds
dev-aws-creds: envsubst
Expand Down
22 changes: 22 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package main
import (
"crypto/tls"
"flag"
"fmt"
"os"
"strings"

hcv2 "github.com/fluxcd/helm-controller/api/v2"
sourcev1 "github.com/fluxcd/source-controller/api/v1"
Expand All @@ -37,8 +39,10 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook"

kcmv1 "github.com/K0rdent/kcm/api/v1alpha1"
"github.com/K0rdent/kcm/internal/build"
"github.com/K0rdent/kcm/internal/controller"
"github.com/K0rdent/kcm/internal/helm"
"github.com/K0rdent/kcm/internal/providers"
"github.com/K0rdent/kcm/internal/telemetry"
"github.com/K0rdent/kcm/internal/utils"
kcmwebhook "github.com/K0rdent/kcm/internal/webhook"
Expand Down Expand Up @@ -115,6 +119,24 @@ func main() {
Development: true,
}
opts.BindFlags(flag.CommandLine)

flag.Usage = func() {
var defaultUsage strings.Builder
{
oldOutput := flag.CommandLine.Output()
flag.CommandLine.SetOutput(&defaultUsage)
flag.PrintDefaults()
flag.CommandLine.SetOutput(oldOutput)
}

_, _ = fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
_, _ = fmt.Fprint(os.Stderr, defaultUsage.String())
_, _ = fmt.Fprintf(os.Stderr, "\nSupported providers:\n")
for _, el := range providers.List() {
_, _ = fmt.Fprintf(os.Stderr, " - %s\n", el)
}
_, _ = fmt.Fprintf(os.Stderr, "\nVersion: %s\n", build.Version)
}
flag.Parse()

ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
Expand Down
13 changes: 7 additions & 6 deletions config/dev/adopted-clusterdeployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ spec:
credential: adopted-cluster-cred
config: {}
serviceSpec:
- template: kyverno-3-2-6
name: kyverno
namespace: kyverno
- template: ingress-nginx-4-11-0
name: ingress-nginx
namespace: ingress-nginx
services:
- template: kyverno-3-2-6
name: kyverno
namespace: kyverno
- template: ingress-nginx-4-11-0
name: ingress-nginx
namespace: ingress-nginx
10 changes: 10 additions & 0 deletions config/dev/adopted-credentials.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ spec:
kind: Secret
name: adopted-cluster-kubeconf
namespace: ${NAMESPACE}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: adopted-cluster-kubeconf-resource-template
namespace: ${NAMESPACE}
labels:
k0rdent.mirantis.com/component: "kcm"
annotations:
projectsveltos.io/template: "true"
10 changes: 10 additions & 0 deletions config/dev/aks-credentials.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,13 @@ spec:
kind: Secret
name: azure-aks-credential
namespace: ${NAMESPACE}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: azure-cluster-identity-resource-template
namespace: ${NAMESPACE}
labels:
k0rdent.mirantis.com/component: "kcm"
annotations:
projectsveltos.io/template: "true"
10 changes: 10 additions & 0 deletions config/dev/aws-credentials.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ spec:
kind: AWSClusterStaticIdentity
name: aws-cluster-identity
namespace: ${NAMESPACE}
---
apiVersion: v1
kind: ConfigMap
metadata:
name: aws-cluster-identity-resource-template
namespace: ${NAMESPACE}
labels:
k0rdent.mirantis.com/component: "kcm"
annotations:
projectsveltos.io/template: "true"
32 changes: 12 additions & 20 deletions internal/providers/azure.go → internal/projectroot/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package providers
package projectroot

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"path/filepath"
"runtime"
)

type ProviderAzure struct{}
// WARNING: This path resolution is dependent on the source file's location
// in the project structure. Moving this file to a different directory will
// change the resolved path. When refactoring or restructuring the project,
// ensure to update this path resolution accordingly.
var (
_, b, _, _ = runtime.Caller(0)

var _ ProviderModule = (*ProviderAzure)(nil)

func init() {
Register(&ProviderAzure{})
}

func (*ProviderAzure) GetName() string {
return "azure"
}

func (*ProviderAzure) GetClusterGVK() schema.GroupVersionKind {
return schema.GroupVersionKind{}
}

func (*ProviderAzure) GetClusterIdentityKinds() []string {
return []string{"AzureClusterIdentity"}
}
// Path is root folder of this project.
Path = filepath.Join(filepath.Dir(b), "../..")
)
43 changes: 0 additions & 43 deletions internal/providers/aws.go

This file was deleted.

33 changes: 19 additions & 14 deletions internal/providers/vsphere.go → internal/providers/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,30 @@
package providers

import (
"k8s.io/apimachinery/pkg/runtime/schema"
)
"cmp"
"fmt"
"os"
"path/filepath"
"testing"

type ProvidervSphere struct{}
"github.com/K0rdent/kcm/internal/projectroot"
)

var _ ProviderModule = (*ProvidervSphere)(nil)
const EnvProvidersPathGlob = "PROVIDERS_PATH_GLOB"

func init() {
Register(&ProvidervSphere{})
}
var baseDir string

func (*ProvidervSphere) GetName() string {
return "vsphere"
}
if testing.Testing() {
baseDir = projectroot.Path
}

func (*ProvidervSphere) GetClusterGVK() schema.GroupVersionKind {
return schema.GroupVersionKind{}
}
providersGlob := cmp.Or(
os.Getenv(EnvProvidersPathGlob),
filepath.Join(baseDir, "providers", "*.yml"),
)

func (*ProvidervSphere) GetClusterIdentityKinds() []string {
return []string{"VSphereClusterIdentity"}
if err := RegisterProvidersFromGlob(providersGlob); err != nil {
panic(fmt.Sprintf("failed to register providers: %v", err))
}
}
39 changes: 0 additions & 39 deletions internal/providers/openstack.go

This file was deleted.

Loading

0 comments on commit 5470743

Please sign in to comment.