Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make namespace configurable for anywhere resources #177

Merged
merged 5 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions controllers/controllers/resource/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ func (r *capiResourceFetcher) fetchClusterForRef(ctx context.Context, refId type
return &c, nil
}
}
if c.Spec.ExternalEtcdConfiguration.MachineGroupRef != nil && c.Spec.ExternalEtcdConfiguration.MachineGroupRef.Name == refId.Name {
if _, err := r.clusterByName(ctx, constants.EksaSystemNamespace, c.Name); err == nil { // further validates a capi cluster exists
return &c, nil
}
}
}
}
return nil, fmt.Errorf("eksa cluster not found for datacenterRef %v", refId)
Expand Down
6 changes: 6 additions & 0 deletions internal/pkg/api/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func WithKubernetesVersion(v v1alpha1.KubernetesVersion) ClusterFiller {
}
}

func WithClusterNamespace(ns string) ClusterFiller {
return func(c *v1alpha1.Cluster) {
c.Namespace = ns
}
}

func WithControlPlaneCount(r int) ClusterFiller {
return func(c *v1alpha1.Cluster) {
c.Spec.ControlPlaneConfiguration.Count = r
Expand Down
6 changes: 6 additions & 0 deletions internal/pkg/api/gitops.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func NewGitOpsConfig(name string, opts ...GitOpsConfigOpt) *v1alpha1.GitOpsConfi
return config
}

func WithGitOpsNamespace(ns string) GitOpsConfigOpt {
return func(c *v1alpha1.GitOpsConfig) {
c.Namespace = ns
}
}

func WithFluxOwner(username string) GitOpsConfigOpt {
return func(c *v1alpha1.GitOpsConfig) {
c.Spec.Flux.Github.Owner = username
Expand Down
11 changes: 11 additions & 0 deletions internal/pkg/api/vsphere.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ func WithStoragePolicyName(value string) VSphereFiller {
}
}

func WithVSphereConfigNamespace(ns string) VSphereFiller {
return func(config VSphereConfig) {
config.datacenterConfig.Namespace = ns
config.workerMachineConfig.Namespace = ns
config.cpMachineConfig.Namespace = ns
if config.etcdMachineConfig != nil {
config.etcdMachineConfig.Namespace = ns
}
}
}

func WithSSHUsernameAndAuthorizedKey(username string, key string) VSphereFiller {
return func(config VSphereConfig) {
if len(config.cpMachineConfig.Spec.Users) == 0 {
Expand Down
10 changes: 7 additions & 3 deletions pkg/api/v1alpha1/gitopsconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (

const GitOpsConfigKind = "GitOpsConfig"

func GetAndValidateGitOpsConfig(fileName string, refName string) (*GitOpsConfig, error) {
func GetAndValidateGitOpsConfig(fileName string, refName string, clusterConfig *Cluster) (*GitOpsConfig, error) {
config, err := getGitOpsConfig(fileName)
if err != nil {
return nil, err
}
err = validateGitOpsConfig(config, refName)
err = validateGitOpsConfig(config, refName, clusterConfig)
if err != nil {
return nil, err
}
Expand All @@ -29,14 +29,18 @@ func getGitOpsConfig(fileName string) (*GitOpsConfig, error) {
return &config, nil
}

func validateGitOpsConfig(config *GitOpsConfig, refName string) error {
func validateGitOpsConfig(config *GitOpsConfig, refName string, clusterConfig *Cluster) error {
if config == nil {
return errors.New("gitOpsRef is specified but GitOpsConfig is not specified")
}
if config.Name != refName {
return fmt.Errorf("GitOpsConfig retrieved with name %s does not match name (%s) specified in "+
"gitOpsRef", config.Name, refName)
}
if config.Namespace != clusterConfig.Namespace {
return errors.New("GitOpsConfig and Cluster objects must have the same namespace specified")
}

flux := config.Spec.Flux

if len(flux.Github.Owner) <= 0 {
Expand Down
12 changes: 11 additions & 1 deletion pkg/api/v1alpha1/gitopsconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func TestGetAndValidateGitOpsConfig(t *testing.T) {
fileName string
refName string
wantGitOpsConfig *GitOpsConfig
clusterConfig *Cluster
wantErr bool
}{
{
Expand Down Expand Up @@ -49,6 +50,15 @@ func TestGetAndValidateGitOpsConfig(t *testing.T) {
},
},
},
clusterConfig: &Cluster{
TypeMeta: metav1.TypeMeta{
Kind: ClusterKind,
APIVersion: SchemeBuilder.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: "default",
},
},
wantErr: false,
},
{
Expand All @@ -67,7 +77,7 @@ func TestGetAndValidateGitOpsConfig(t *testing.T) {

for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
got, err := GetAndValidateGitOpsConfig(tt.fileName, tt.refName)
got, err := GetAndValidateGitOpsConfig(tt.fileName, tt.refName, tt.clusterConfig)
if (err != nil) != tt.wantErr {
t.Fatalf("GetAndValidateGitOpsConfig() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down
9 changes: 6 additions & 3 deletions pkg/api/v1alpha1/oidcconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (

const OIDCConfigKind = "OIDCConfig"

func GetAndValidateOIDCConfig(fileName string, refName string) (*OIDCConfig, error) {
func GetAndValidateOIDCConfig(fileName string, refName string, clusterConfig *Cluster) (*OIDCConfig, error) {
config, err := getOIDCConfig(fileName)
if err != nil {
return nil, err
}
err = validateOIDCConfig(config, refName)
err = validateOIDCConfig(config, refName, clusterConfig)
if err != nil {
return nil, err
}
Expand All @@ -32,14 +32,17 @@ func getOIDCConfig(fileName string) (*OIDCConfig, error) {
return &config, nil
}

func validateOIDCConfig(config *OIDCConfig, refName string) error {
func validateOIDCConfig(config *OIDCConfig, refName string, clusterConfig *Cluster) error {
if config == nil {
return nil
}
if config.Name != refName {
return fmt.Errorf("OIDCConfig retrieved with name %v does not match name (%v) specified in "+
"identityProviderRefs", config.Name, refName)
}
if config.Namespace != clusterConfig.Namespace {
return fmt.Errorf("OIDCConfig and Cluster objects must have the same namespace specified")
}
if config.Spec.ClientId == "" {
return fmt.Errorf("OIDCConfig clientId is required")
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/v1alpha1/oidcconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ func TestGetAndValidateOIDCConfig(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
got, err := GetAndValidateOIDCConfig(tt.fileName, tt.refName)
c := &Cluster{}
got, err := GetAndValidateOIDCConfig(tt.fileName, tt.refName, c)
if (err != nil) != tt.wantErr {
t.Fatalf("GetAndValidateOIDCConfig() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down
8 changes: 6 additions & 2 deletions pkg/bootstrapper/bootstrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type ClusterClient interface {
ClusterExists(ctx context.Context, clusterName string) (bool, error)
ValidateClustersCRD(ctx context.Context, cluster *types.Cluster) error
CreateNamespace(ctx context.Context, kubeconfig string, namespace string) error
GetNamespace(ctx context.Context, kubeconfig string, namespace string) error
}

type (
Expand All @@ -51,8 +52,11 @@ func (b *Bootstrapper) CreateBootstrapCluster(ctx context.Context, clusterSpec *
KubeconfigFile: kubeconfigFile,
}

if err := b.clusterClient.CreateNamespace(ctx, c.KubeconfigFile, constants.EksaSystemNamespace); err != nil {
return nil, err
err = b.clusterClient.GetNamespace(ctx, c.KubeconfigFile, constants.EksaSystemNamespace)
if err != nil {
if err := b.clusterClient.CreateNamespace(ctx, c.KubeconfigFile, constants.EksaSystemNamespace); err != nil {
return nil, err
}
}

err = cluster.ApplyExtraObjects(ctx, b.clusterClient, c, clusterSpec)
Expand Down
4 changes: 2 additions & 2 deletions pkg/bootstrapper/bootstrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestBootstrapperCreateBootstrapClusterSuccessNoExtraObjects(t *testing.T) {
ctx := context.Background()
b, client := newBootstrapper(t)
client.EXPECT().CreateBootstrapCluster(ctx, clusterSpec).Return(kubeconfigFile, nil)
client.EXPECT().CreateNamespace(ctx, kubeconfigFile, constants.EksaSystemNamespace)
client.EXPECT().GetNamespace(ctx, kubeconfigFile, constants.EksaSystemNamespace)

got, err := b.CreateBootstrapCluster(ctx, clusterSpec, tt.opts...)
if err != nil {
Expand All @@ -60,7 +60,7 @@ func TestBootstrapperCreateBootstrapClusterSuccessExtraObjects(t *testing.T) {
ctx := context.Background()
b, client := newBootstrapper(t)
client.EXPECT().CreateBootstrapCluster(ctx, clusterSpec).Return(kubeconfigFile, nil)
client.EXPECT().CreateNamespace(ctx, kubeconfigFile, constants.EksaSystemNamespace)
client.EXPECT().GetNamespace(ctx, kubeconfigFile, constants.EksaSystemNamespace)
client.EXPECT().ApplyKubeSpecFromBytes(ctx, wantCluster, gomock.Any())

got, err := b.CreateBootstrapCluster(ctx, clusterSpec)
Expand Down
14 changes: 14 additions & 0 deletions pkg/bootstrapper/mocks/client.go

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

4 changes: 2 additions & 2 deletions pkg/cluster/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ func NewSpec(clusterConfigPath string, cliVersion version.Info, opts ...SpecOpt)
if len(s.Cluster.Spec.IdentityProviderRefs) != 0 {
// Since we only support one configuration, and only OIDCConfig, for identityProviderRefs, it is safe to assume that
// it is the only element that exists in the array
oidcConfig, err := eksav1alpha1.GetAndValidateOIDCConfig(clusterConfigPath, s.Cluster.Spec.IdentityProviderRefs[0].Name)
oidcConfig, err := eksav1alpha1.GetAndValidateOIDCConfig(clusterConfigPath, s.Cluster.Spec.IdentityProviderRefs[0].Name, clusterConfig)
if err != nil {
return nil, err
}
s.OIDCConfig = oidcConfig
}

if s.Cluster.Spec.GitOpsRef != nil {
gitOpsConfig, err := eksav1alpha1.GetAndValidateGitOpsConfig(clusterConfigPath, s.Cluster.Spec.GitOpsRef.Name)
gitOpsConfig, err := eksav1alpha1.GetAndValidateGitOpsConfig(clusterConfigPath, s.Cluster.Spec.GitOpsRef.Name, clusterConfig)
if err != nil {
return nil, err
}
Expand Down
Loading