Skip to content

Commit

Permalink
Automate creation of repository server CRD (#2064)
Browse files Browse the repository at this point in the history
* automate creation of repo server CRD

* address review comments

* add symbolic link
  • Loading branch information
kale-amruta committed May 19, 2023
1 parent 15140cc commit fa15d49
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 23 deletions.
13 changes: 12 additions & 1 deletion cmd/reposervercontroller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package main

import (
"context"
"flag"
"os"

Expand All @@ -32,6 +33,7 @@ import (
crkanisteriov1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
"github.com/kanisterio/kanister/pkg/controllers/repositoryserver"
"github.com/kanisterio/kanister/pkg/log"
"github.com/kanisterio/kanister/pkg/resource"
//+kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -64,7 +66,8 @@ func main() {
flag.Parse()
logger := zap.New(zap.UseFlagOptions(&opts))
ctrl.SetLogger(logger)
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
config := ctrl.GetConfigOrDie()
mgr, err := ctrl.NewManager(config, ctrl.Options{
Scheme: scheme,
MetricsBindAddress: metricsAddr,
Port: 9443,
Expand Down Expand Up @@ -94,6 +97,14 @@ func main() {
os.Exit(1)
}

// CRDs should only be created/updated if the env var CREATEORUPDATE_CRDS is set to true
if resource.CreateOrUpdateCRDs() {
if err := resource.CreateRepoServerCustomResource(context.Background(), config); err != nil {
setupLog.Error(err, "Failed to create CustomResources.")
os.Exit(1)
}
}

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
setupLog.Error(err, "problem running manager")
Expand Down
1 change: 1 addition & 0 deletions helm/kanister-operator/crds/repositoryserver.yaml
2 changes: 2 additions & 0 deletions helm/kanister-operator/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ spec:
value: {{ .Values.repositoryServerController.serverStartTimeout | quote }}
- name: LOG_LEVEL
value: {{ .Values.repositoryServerController.logLevel }}
- name: CREATEORUPDATE_CRDS
value: {{ .Values.controller.updateCRDs | quote }}
{{- if .Values.resources }}
resources:
{{ toYaml .Values.resources | indent 12 }}
Expand Down
10 changes: 10 additions & 0 deletions pkg/apis/cr/v1alpha1/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,16 @@ var ProfileResource = customresource.CustomResource{
Kind: reflect.TypeOf(Profile{}).Name(),
}

// RepositoryServerResource is a CRD for blueprints.
var RepositoryServerResource = customresource.CustomResource{
Name: consts.RepositoryServerResourceName,
Plural: consts.RepositoryServerResourceNamePlural,
Group: ResourceGroup,
Version: SchemeVersion,
Scope: apiextensionsv1.NamespaceScoped,
Kind: reflect.TypeOf(RepositoryServer{}).Name(),
}

// Kind takes an unqualified kind and returns back a Group qualified GroupKind
func Kind(kind string) schema.GroupKind {
return SchemeGroupVersion.WithKind(kind).GroupKind()
Expand Down
4 changes: 4 additions & 0 deletions pkg/consts/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ const (
ProfileResourceNamePlural = "profiles"
)

// These consts are used to query Repository server API objects
const RepositoryServerResourceName = "repositoryserver"
const RepositoryServerResourceNamePlural = "repositoryservers"

const LatestKanisterToolsImage = "ghcr.io/kanisterio/kanister-tools:v9.99.9-dev"
const KanisterToolsImage = "ghcr.io/kanisterio/kanister-tools:0.91.0"
23 changes: 1 addition & 22 deletions pkg/kancontroller/kancontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,18 @@ import (
"fmt"
"os"
"os/signal"
"strconv"
"syscall"

"k8s.io/client-go/rest"

"github.com/kanisterio/kanister/pkg/controller"
"github.com/kanisterio/kanister/pkg/field"
_ "github.com/kanisterio/kanister/pkg/function"
"github.com/kanisterio/kanister/pkg/handler"
"github.com/kanisterio/kanister/pkg/kube"
"github.com/kanisterio/kanister/pkg/log"
"github.com/kanisterio/kanister/pkg/resource"
)

const (
createOrUpdateCRDEnvVar = "CREATEORUPDATE_CRDS"
)

func Execute() {
ctx := context.Background()
logLevel, exists := os.LookupEnv(log.LevelEnvName)
Expand Down Expand Up @@ -84,7 +78,7 @@ func Execute() {
}

// CRDs should only be created/updated if the env var CREATEORUPDATE_CRDS is set to true
if createOrUpdateCRDs() {
if resource.CreateOrUpdateCRDs() {
if err := resource.CreateCustomResources(ctx, config); err != nil {
log.WithError(err).Print("Failed to create CustomResources.")
return
Expand Down Expand Up @@ -124,18 +118,3 @@ func isCACertMounted() bool {

return true
}

func createOrUpdateCRDs() bool {
createOrUpdateCRD := os.Getenv(createOrUpdateCRDEnvVar)
if createOrUpdateCRD == "" {
return true
}

c, err := strconv.ParseBool(createOrUpdateCRD)
if err != nil {
log.Print("environment variable", field.M{"CREATEORUPDATE_CRDS": createOrUpdateCRD})
return true
}

return c
}
37 changes: 37 additions & 0 deletions pkg/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ package resource

import (
"context"
"log"
"os"
"strconv"
"time"

"github.com/pkg/errors"
Expand All @@ -26,6 +29,11 @@ import (

crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
customresource "github.com/kanisterio/kanister/pkg/customresource"
"github.com/kanisterio/kanister/pkg/field"
)

const (
createOrUpdateCRDEnvVar = "CREATEORUPDATE_CRDS"
)

// CreateCustomResources creates the given custom resources and waits for them to initialize
Expand Down Expand Up @@ -58,3 +66,32 @@ func newOpKitContext(config *rest.Config) (*customresource.Context, error) {
Timeout: 60 * time.Second,
}, nil
}

// CreateRepoServerCustomResource creates the kopia repository server custom resource
func CreateRepoServerCustomResource(ctx context.Context, config *rest.Config) error {
crCTX, err := newOpKitContext(config)
if err != nil {
return err
}

resources := []customresource.CustomResource{
crv1alpha1.RepositoryServerResource,
}

return customresource.CreateCustomResources(*crCTX, resources)
}

func CreateOrUpdateCRDs() bool {
createOrUpdateCRD := os.Getenv(createOrUpdateCRDEnvVar)
if createOrUpdateCRD == "" {
return true
}

c, err := strconv.ParseBool(createOrUpdateCRD)
if err != nil {
log.Print("environment variable", field.M{"CREATEORUPDATE_CRDS": createOrUpdateCRD})
return true
}

return c
}

0 comments on commit fa15d49

Please sign in to comment.