-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apply crds on operator startup (#122)
- Loading branch information
1 parent
d6fd174
commit 948afca
Showing
7 changed files
with
198 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/Azure/aks-app-routing-operator/pkg/config" | ||
"github.com/Azure/aks-app-routing-operator/pkg/util" | ||
"github.com/go-logr/logr" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
// loadCRDs loads the CRDs from the specified path into the cluster | ||
func loadCRDs(c client.Client, cfg *config.Config, log logr.Logger) error { | ||
if cfg == nil { | ||
return errors.New("config cannot be nil") | ||
} | ||
|
||
log = log.WithValues("crdPath", cfg.CrdPath) | ||
log.Info("reading crd directory") | ||
files, err := os.ReadDir(cfg.CrdPath) | ||
if err != nil { | ||
return fmt.Errorf("reading crd directory %s: %w", cfg.CrdPath, err) | ||
} | ||
|
||
log.Info("applying crds") | ||
for _, file := range files { | ||
if file.IsDir() { | ||
continue | ||
} | ||
|
||
path := filepath.Join(cfg.CrdPath, file.Name()) | ||
log := log.WithValues("path", path) | ||
log.Info("reading crd file") | ||
var content []byte | ||
content, err := os.ReadFile(path) | ||
if err != nil { | ||
return fmt.Errorf("reading crd file %s: %w", path, err) | ||
} | ||
|
||
log.Info("unmarshalling crd file") | ||
crd := &apiextensionsv1.CustomResourceDefinition{} | ||
if err := yaml.Unmarshal(content, crd); err != nil { | ||
return fmt.Errorf("unmarshalling crd file %s: %w", path, err) | ||
} | ||
|
||
log.Info("upserting crd") | ||
if err := util.Upsert(context.Background(), c, crd); err != nil { | ||
return fmt.Errorf("upserting crd %s: %w", crd.Name, err) | ||
} | ||
} | ||
|
||
log.Info("crds loaded") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/Azure/aks-app-routing-operator/pkg/config" | ||
"github.com/go-logr/logr" | ||
"github.com/stretchr/testify/require" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
) | ||
|
||
const ( | ||
validCrdPath = "../../config/crd/bases/" | ||
validCrdName = "nginxingresscontrollers.approuting.kubernetes.azure.com" | ||
validCrdPathWithDir = "../../config/crd/" | ||
|
||
nonCrdManifestsPath = "../manifests/fixtures/nginx" | ||
nonExistentFilePath = "./this/does/not/exist" | ||
) | ||
|
||
func TestLoadCRDs(t *testing.T) { | ||
t.Run("valid crds", func(t *testing.T) { | ||
cl := fake.NewClientBuilder().WithScheme(scheme).Build() | ||
require.NoError(t, loadCRDs(cl, &config.Config{CrdPath: validCrdPath}, logr.Discard()), "expected no error loading valid crds") | ||
|
||
crd := &apiextensionsv1.CustomResourceDefinition{} | ||
crd.Name = validCrdName | ||
require.NoError(t, cl.Get(context.Background(), client.ObjectKeyFromObject(crd), crd, nil), "getting loaded valid crd") | ||
}) | ||
|
||
t.Run("valid crds with directory", func(t *testing.T) { | ||
cl := fake.NewClientBuilder().WithScheme(scheme).Build() | ||
require.NoError(t, loadCRDs(cl, &config.Config{CrdPath: validCrdPath}, logr.Discard()), "expected no error loading valid crds") | ||
}) | ||
|
||
t.Run("invalid crds", func(t *testing.T) { | ||
cl := fake.NewClientBuilder().WithScheme(scheme).Build() | ||
err := loadCRDs(cl, &config.Config{CrdPath: nonCrdManifestsPath}, logr.Discard()) | ||
require.Error(t, err, "expected error loading invalid crds") | ||
require.True(t, strings.Contains(err.Error(), "unmarshalling crd file"), "expected error to be about umarshalling crd") | ||
}) | ||
|
||
t.Run("non-existent crd path", func(t *testing.T) { | ||
cl := fake.NewClientBuilder().WithScheme(scheme).Build() | ||
err := loadCRDs(cl, &config.Config{CrdPath: nonExistentFilePath}, logr.Discard()) | ||
require.Error(t, err, "expected error loading non-existent crd path") | ||
require.True(t, strings.Contains(err.Error(), "reading crd directory"), "expected error to be about reading crd directory") | ||
}) | ||
|
||
t.Run("nil config", func(t *testing.T) { | ||
cl := fake.NewClientBuilder().WithScheme(scheme).Build() | ||
err := loadCRDs(cl, nil, logr.Discard()) | ||
require.Error(t, err, "expected error loading nil config") | ||
require.True(t, strings.Contains(err.Error(), "config cannot be nil"), "expected error to be about nil config") | ||
}) | ||
|
||
} |