Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
gitopsv1alpha1 "github.com/hybrid-cloud-patterns/patterns-operator/api/v1alpha1"
controllers "github.com/hybrid-cloud-patterns/patterns-operator/internal/controller"
"github.com/hybrid-cloud-patterns/patterns-operator/version"
configv1 "github.com/openshift/api/config/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand All @@ -58,6 +59,7 @@ func init() {
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

utilruntime.Must(gitopsv1alpha1.AddToScheme(scheme))
utilruntime.Must(configv1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}

Expand Down
2 changes: 1 addition & 1 deletion config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ kind: Kustomization
images:
- name: controller
newName: quay.io/validatedpatterns/patterns-operator
newTag: 0.0.63
newTag: 0.0.64
13 changes: 13 additions & 0 deletions config/samples/test-pattern.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: gitops.hybrid-cloud-patterns.io/v1alpha1
kind: Pattern
metadata:
name: test-pattern-infra-params
namespace: default
spec:
clusterGroupName: test-group
gitSpec:
targetRepo: "https://github.com/validatedpatterns/multicloud-gitops"
targetRevision: "main"
multiSourceConfig:
enabled: false
helmRepoUrl: "https://charts.validatedpatterns.io/"
96 changes: 0 additions & 96 deletions hack/operator-build-deploy.sh
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't delete this one, we love it ;)

This file was deleted.

43 changes: 29 additions & 14 deletions internal/controller/argo.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"strconv"
"strings"

configv1 "github.com/openshift/api/config/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -347,7 +348,7 @@ func getArgoCD(client dynamic.Interface, name, namespace string) (*argooperator.
return argo, err
}

func newApplicationParameters(p *api.Pattern) []argoapi.HelmParameter {
func newApplicationParameters(p *api.Pattern, infra *configv1.Infrastructure) []argoapi.HelmParameter {
parameters := []argoapi.HelmParameter{
{
Name: "global.pattern",
Expand Down Expand Up @@ -411,6 +412,20 @@ func newApplicationParameters(p *api.Pattern) []argoapi.HelmParameter {
Value: p.Spec.ExperimentalCapabilities,
},
}
if infra != nil {
log.Printf("Adding infrastructure parameters: clusterAPIServerURL=%s, controlPlaneTopology=%s", infra.Status.APIServerURL, infra.Status.ControlPlaneTopology)
parameters = append(parameters, argoapi.HelmParameter{
Name: "global.clusterAPIServerURL",
Value: infra.Status.APIServerURL,
},
argoapi.HelmParameter{
Name: "global.controlPlaneTopology",
Value: string(infra.Status.ControlPlaneTopology),
},
)
} else {
log.Printf("Warning: infra is nil, skipping infrastructure parameters (global.clusterAPIServerURL and global.controlPlaneTopology)")
}
parameters = append(parameters, argoapi.HelmParameter{
Name: "global.multiSourceTargetRevision",
Value: getClusterGroupChartVersion(p),
Expand Down Expand Up @@ -491,7 +506,7 @@ func newApplicationValues(p *api.Pattern) string {
// libraries. E.g. a string '/overrides/values-{{ $.Values.global.clusterPlatform }}.yaml'
// will be converted to '/overrides/values-AWS.yaml'
// 4. We return the list of templated strings back as an array
func getSharedValueFiles(p *api.Pattern, prefix string) ([]string, error) {
func getSharedValueFiles(p *api.Pattern, prefix string, infra *configv1.Infrastructure) ([]string, error) {
gitDir := p.Status.LocalCheckoutPath
if _, err := os.Stat(gitDir); err != nil {
return nil, fmt.Errorf("%s path does not exist", gitDir)
Expand Down Expand Up @@ -521,7 +536,7 @@ func getSharedValueFiles(p *api.Pattern, prefix string) ([]string, error) {
if !ok {
return nil, fmt.Errorf("type assertion failed at index %d: Not a string", i)
}
valueMap := convertArgoHelmParametersToMap(newApplicationParameters(p))
valueMap := convertArgoHelmParametersToMap(newApplicationParameters(p, infra))
templatedString, err := helmTpl(str, valueFiles, valueMap)

// we only log an error, but try to keep going
Expand Down Expand Up @@ -595,9 +610,9 @@ func commonApplicationSpec(p *api.Pattern, sources []argoapi.ApplicationSource)
return spec
}

func commonApplicationSourceHelm(p *api.Pattern, prefix string) *argoapi.ApplicationSourceHelm {
func commonApplicationSourceHelm(p *api.Pattern, prefix string, infra *configv1.Infrastructure) *argoapi.ApplicationSourceHelm {
valueFiles := newApplicationValueFiles(p, prefix)
sharedValueFiles, err := getSharedValueFiles(p, prefix)
sharedValueFiles, err := getSharedValueFiles(p, prefix, infra)
if err != nil {
fmt.Printf("Could not fetch sharedValueFiles: %s", err)
}
Expand All @@ -607,7 +622,7 @@ func commonApplicationSourceHelm(p *api.Pattern, prefix string) *argoapi.Applica
ValueFiles: valueFiles,

// Parameters is a list of Helm parameters which are passed to the helm template command upon manifest generation
Parameters: newApplicationParameters(p),
Parameters: newApplicationParameters(p, infra),

// This is to be able to pass down the extraParams to the single applications
Values: newApplicationValues(p),
Expand Down Expand Up @@ -644,7 +659,7 @@ func newArgoOperatorApplication(p *api.Pattern, spec *argoapi.ApplicationSpec) *
return &app
}

func newSourceApplication(p *api.Pattern) *argoapi.Application {
func newSourceApplication(p *api.Pattern, infra *configv1.Infrastructure) *argoapi.Application {
// Argo uses...
// r := regexp.MustCompile("(/|:)")
// root := filepath.Join(os.TempDir(), r.ReplaceAllString(NormalizeGitURL(rawRepoURL), "_"))
Expand All @@ -653,15 +668,15 @@ func newSourceApplication(p *api.Pattern) *argoapi.Application {
RepoURL: p.Spec.GitConfig.TargetRepo,
Path: "common/clustergroup",
TargetRevision: p.Spec.GitConfig.TargetRevision,
Helm: commonApplicationSourceHelm(p, ""),
Helm: commonApplicationSourceHelm(p, "", infra),
}
spec := commonApplicationSpec(p, []argoapi.ApplicationSource{source})

spec.SyncPolicy = commonSyncPolicy(p)
return newArgoOperatorApplication(p, spec)
}

func newMultiSourceApplication(p *api.Pattern) *argoapi.Application {
func newMultiSourceApplication(p *api.Pattern, infra *configv1.Infrastructure) *argoapi.Application {
sources := []argoapi.ApplicationSource{}
var baseSource *argoapi.ApplicationSource

Expand All @@ -681,14 +696,14 @@ func newMultiSourceApplication(p *api.Pattern) *argoapi.Application {
RepoURL: p.Spec.MultiSourceConfig.HelmRepoUrl,
Chart: "clustergroup",
TargetRevision: getClusterGroupChartVersion(p),
Helm: commonApplicationSourceHelm(p, "$patternref"),
Helm: commonApplicationSourceHelm(p, "$patternref", infra),
}
} else {
baseSource = &argoapi.ApplicationSource{
RepoURL: p.Spec.MultiSourceConfig.ClusterGroupGitRepoUrl,
Path: ".",
TargetRevision: p.Spec.MultiSourceConfig.ClusterGroupChartGitRevision,
Helm: commonApplicationSourceHelm(p, "$patternref"),
Helm: commonApplicationSourceHelm(p, "$patternref", infra),
}
}
sources = append(sources, *baseSource)
Expand All @@ -712,14 +727,14 @@ func getClusterGroupChartVersion(p *api.Pattern) string {
return clusterGroupChartVersion
}

func newArgoApplication(p *api.Pattern) *argoapi.Application {
func newArgoApplication(p *api.Pattern, infra *configv1.Infrastructure) *argoapi.Application {
// -- ArgoCD Application
var targetApp *argoapi.Application

if *p.Spec.MultiSourceConfig.Enabled {
targetApp = newMultiSourceApplication(p)
targetApp = newMultiSourceApplication(p, infra)
} else {
targetApp = newSourceApplication(p)
targetApp = newSourceApplication(p, infra)
}

return targetApp
Expand Down
16 changes: 8 additions & 8 deletions internal/controller/argo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ var _ = Describe("Argo Pattern", func() {
TargetRevision: pattern.Spec.GitConfig.TargetRevision,
Helm: &argoapi.ApplicationSourceHelm{
ValueFiles: newApplicationValueFiles(pattern, ""),
Parameters: newApplicationParameters(pattern),
Parameters: newApplicationParameters(pattern, nil),
Values: newApplicationValues(pattern),
IgnoreMissingValueFiles: true,
},
Expand Down Expand Up @@ -137,7 +137,7 @@ var _ = Describe("Argo Pattern", func() {
// This is needed to debug any failures as gomega truncates the diff output
format.MaxDepth = 100
format.MaxLength = 0
Expect(newArgoApplication(pattern)).To(Equal(argoApp))
Expect(newArgoApplication(pattern, nil)).To(Equal(argoApp))
})
})
Context("Default multi source", func() {
Expand All @@ -160,7 +160,7 @@ var _ = Describe("Argo Pattern", func() {
*appSource,
}
multiSourceArgoApp.Spec.Sources[1].Helm.ValueFiles = newApplicationValueFiles(pattern, "$patternref")
Expect(newMultiSourceApplication(pattern)).To(Equal(multiSourceArgoApp))
Expect(newMultiSourceApplication(pattern, nil)).To(Equal(multiSourceArgoApp))
})
})
Context("multiSource with MultiSourceClusterGroupChartGitRevision set", func() {
Expand All @@ -184,7 +184,7 @@ var _ = Describe("Argo Pattern", func() {
*appSource,
}
multiSourceArgoApp.Spec.Sources[1].Helm.ValueFiles = newApplicationValueFiles(pattern, "$patternref")
Expect(newMultiSourceApplication(pattern)).To(Equal(multiSourceArgoApp))
Expect(newMultiSourceApplication(pattern, nil)).To(Equal(multiSourceArgoApp))
})
})
})
Expand Down Expand Up @@ -405,7 +405,7 @@ var _ = Describe("Argo Pattern", func() {
}
})
It("Test default newApplicationParameters", func() {
Expect(newApplicationParameters(pattern)).To(Equal(append(appParameters,
Expect(newApplicationParameters(pattern, nil)).To(Equal(append(appParameters,
argoapi.HelmParameter{
Name: "global.multiSourceSupport",
Value: "false",
Expand Down Expand Up @@ -439,7 +439,7 @@ var _ = Describe("Argo Pattern", func() {
Value: "test2value",
},
}
Expect(newApplicationParameters(pattern)).To(Equal(append(appParameters,
Expect(newApplicationParameters(pattern, nil)).To(Equal(append(appParameters,
argoapi.HelmParameter{
Name: "global.multiSourceSupport",
Value: "false",
Expand Down Expand Up @@ -472,7 +472,7 @@ var _ = Describe("Argo Pattern", func() {
It("Test newApplicationParameters with multiSource", func() {
tmpBool := true
pattern.Spec.MultiSourceConfig.Enabled = &tmpBool
Expect(newApplicationParameters(pattern)).To(Equal(append(appParameters,
Expect(newApplicationParameters(pattern, nil)).To(Equal(append(appParameters,
argoapi.HelmParameter{
Name: "global.multiSourceSupport",
Value: "true",
Expand Down Expand Up @@ -500,7 +500,7 @@ var _ = Describe("Argo Pattern", func() {
var sources []argoapi.ApplicationSource

BeforeEach(func() {
multiSourceArgoApp = newMultiSourceApplication(pattern)
multiSourceArgoApp = newMultiSourceApplication(pattern, nil)
sources = multiSourceArgoApp.Spec.Sources
})
It("compareSource() function identical", func() {
Expand Down
24 changes: 22 additions & 2 deletions internal/controller/pattern_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -40,6 +41,7 @@ import (

argoapi "github.com/argoproj/argo-cd/v3/pkg/apis/application/v1alpha1"
argoclient "github.com/argoproj/argo-cd/v3/pkg/client/clientset/versioned"
configv1 "github.com/openshift/api/config/v1"
configclient "github.com/openshift/client-go/config/clientset/versioned"
routeclient "github.com/openshift/client-go/route/clientset/versioned"
olmclient "github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
Expand Down Expand Up @@ -223,7 +225,17 @@ func (r *PatternReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
return r.actionPerformed(qualifiedInstance, ret, err)
}

targetApp := newArgoApplication(qualifiedInstance)
// Fetch infrastructure for cluster API server URL and control plane topology
infra := &configv1.Infrastructure{}
if err := r.Get(ctx, types.NamespacedName{Name: "cluster"}, infra); err != nil {
// If infrastructure cannot be fetched, continue with nil (handled in newApplicationParameters)
log.Printf("Warning: Could not fetch infrastructure object: %v. Parameters global.clusterAPIServerURL and global.controlPlaneTopology will not be set.", err)
infra = nil
} else {
log.Printf("Successfully fetched infrastructure: APIServerURL=%s, ControlPlaneTopology=%s", infra.Status.APIServerURL, infra.Status.ControlPlaneTopology)
}

targetApp := newArgoApplication(qualifiedInstance, infra)
_ = controllerutil.SetOwnerReference(qualifiedInstance, targetApp, r.Scheme)
app, err := getApplication(r.argoClient, applicationName(qualifiedInstance), clusterWideNS)
if app == nil {
Expand Down Expand Up @@ -510,7 +522,15 @@ func (r *PatternReconciler) finalizeObject(instance *api.Pattern) error {
}
ns := getClusterWideArgoNamespace()

targetApp := newArgoApplication(qualifiedInstance)
// Fetch infrastructure for cluster API server URL and control plane topology
infra := &configv1.Infrastructure{}
if err := r.Get(context.TODO(), types.NamespacedName{Name: "cluster"}, infra); err != nil {
// If infrastructure cannot be fetched, continue with nil (handled in newApplicationParameters)
log.Printf("Warning: Could not fetch infrastructure object during finalization: %v. Parameters global.clusterAPIServerURL and global.controlPlaneTopology will not be set.", err)
infra = nil
}

targetApp := newArgoApplication(qualifiedInstance, infra)
_ = controllerutil.SetOwnerReference(qualifiedInstance, targetApp, r.Scheme)

app, _ := getApplication(r.argoClient, applicationName(qualifiedInstance), ns)
Expand Down