Skip to content

Commit 4593b72

Browse files
authored
feat(operator): support private OCI registry credentials for platform source (#2472)
## Summary Implements support for private OCI registry and Git repository credentials in platform sources. Adds `--platform-source-secret` flag to the operator and corresponding Helm chart parameter. Closes #2455 ## Changes ### Operator (`cmd/cozystack-operator/main.go`) - Added `--platform-source-secret` flag for specifying Secret name - Updated `generateOCIRepository()` to set `spec.secretRef` when secret is provided - Updated `generateGitRepository()` to set `spec.secretRef` when secret is provided - Secret is expected to be in `cozy-system` namespace ### Helm Chart - **`packages/core/installer/values.yaml`**: Added `cozystackOperator.platformSourceSecret` parameter with documentation - **`packages/core/installer/templates/cozystack-operator.yaml`**: Pass `--platform-source-secret` flag to operator container ## Usage Example ### For private OCI registry (e.g., GitLab Container Registry): ```bash # Create registry credentials secret kubectl create secret docker-registry gitlab-registry-creds \ --namespace cozy-system \ --docker-server=registry.gitlab.example.com \ --docker-username=<username> \ --docker-password=<token> ``` ```yaml # Helm values cozystackOperator: platformSourceUrl: 'oci://registry.gitlab.example.com/mygroup/cozystack-packages' platformSourceRef: 'tag=v1.0.0' platformSourceSecret: 'gitlab-registry-creds' ``` ### For private Git repository: ```bash # Create Git credentials secret (SSH key or basic auth) kubectl create secret generic git-creds \ --namespace cozy-system \ --from-literal=username=<username> \ --from-literal=password=<token> ``` ```yaml # Helm values cozystackOperator: platformSourceUrl: 'https://github.com/private-org/cozystack-packages' platformSourceRef: 'branch=main' platformSourceSecret: 'git-creds' ``` ## Testing Manually verified: - OCIRepository resource is created with `spec.secretRef` when secret name is provided - GitRepository resource is created with `spec.secretRef` when secret name is provided - Resources are created without `spec.secretRef` when secret name is empty (backward compatible) - Helm chart renders correctly with and without `platformSourceSecret` parameter ## Checklist - [x] Code follows project style and conventions - [x] Operator supports both OCI and Git source types - [x] Helm chart parameter documented with comments - [x] Backward compatible (empty secret name is handled correctly) - [x] Closes referenced issue (#2455) ## Release note ```release-note feat(operator): add `--platform-source-secret` flag to authenticate the platform package source against private OCI registries (kubernetes.io/dockerconfigjson) and private Git repositories (basic-auth, bearer token, or SSH key). Empty value preserves existing public-registry behaviour. ``` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added support for configuring a Kubernetes Secret for the platform source. * Installer settings and startup options now let you provide a secret name for Flux source credentials. * **Bug Fixes** * Generated platform source resources can now reference the provided Secret, enabling authenticated access for OCI and Git sources. * **Documentation** * Updated configuration docs to explain the new secret setting and required secret format. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2 parents fef9e76 + fff9e82 commit 4593b72

5 files changed

Lines changed: 76 additions & 14 deletions

File tree

‎cmd/cozystack-operator/main.go‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030

3131
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
3232
helmv2 "github.com/fluxcd/helm-controller/api/v2"
33+
fluxmeta "github.com/fluxcd/pkg/apis/meta"
3334
sourcev1 "github.com/fluxcd/source-controller/api/v1"
3435
sourcewatcherv1beta1 "github.com/fluxcd/source-watcher/api/v2/v1beta1"
3536
corev1 "k8s.io/api/core/v1"
@@ -95,6 +96,7 @@ func main() {
9596
var platformSourceURL string
9697
var platformSourceName string
9798
var platformSourceRef string
99+
var platformSourceSecret string
98100

99101
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
100102
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
@@ -138,6 +140,7 @@ func main() {
138140
flag.StringVar(&platformSourceURL, "platform-source-url", "", "Platform source URL (oci:// or https://). If specified, generates OCIRepository or GitRepository resource.")
139141
flag.StringVar(&platformSourceName, "platform-source-name", "cozystack-platform", "Name for the generated platform source resource and PackageSource")
140142
flag.StringVar(&platformSourceRef, "platform-source-ref", "", "Reference specification as key=value pairs (e.g., 'branch=main' or 'digest=sha256:...,tag=v1.0'). For OCI: digest, semver, semverFilter, tag. For Git: branch, tag, semver, name, commit.")
143+
flag.StringVar(&platformSourceSecret, "platform-source-secret", "", "Name of the Secret in cozy-system namespace containing credentials for the platform source. Sets spec.secretRef on the generated OCIRepository or GitRepository. Secret type depends on the source kind: kubernetes.io/dockerconfigjson for OCI (oci://...); Opaque with username+password (or bearerToken) for Git over HTTPS; Opaque with identity (PEM private key) and known_hosts for Git over SSH.")
141144
flag.StringVar(&cozyValuesSecretName, "cozy-values-secret-name", "cozystack-values", "The name of the secret containing cluster-wide configuration values.")
142145
flag.StringVar(&cozyValuesSecretNamespace, "cozy-values-secret-namespace", "cozy-system", "The namespace of the secret containing cluster-wide configuration values.")
143146
flag.StringVar(&cozyValuesNamespaceSelector, "cozy-values-namespace-selector", "cozystack.io/system=true", "The label selector for namespaces where the cluster-wide configuration values must be replicated.")
@@ -256,12 +259,12 @@ func main() {
256259

257260
// Generate and install platform source resource if specified
258261
if platformSourceURL != "" {
259-
setupLog.Info("Generating platform source resource", "url", platformSourceURL, "name", platformSourceName, "ref", platformSourceRef)
262+
setupLog.Info("Generating platform source resource", "url", platformSourceURL, "name", platformSourceName, "ref", platformSourceRef, "secret", platformSourceSecret)
260263
installCtx, installCancel := context.WithTimeout(mgrCtx, 2*time.Minute)
261264
defer installCancel()
262265

263266
// Use direct client for pre-start operations (cache is not ready yet)
264-
if err := installPlatformSourceResource(installCtx, directClient, platformSourceURL, platformSourceName, platformSourceRef); err != nil {
267+
if err := installPlatformSourceResource(installCtx, directClient, platformSourceURL, platformSourceName, platformSourceRef, platformSourceSecret); err != nil {
265268
setupLog.Error(err, "failed to install platform source resource")
266269
os.Exit(1)
267270
} else {
@@ -375,7 +378,7 @@ func main() {
375378

376379
// installPlatformSourceResource generates and installs a Flux source resource (OCIRepository or GitRepository)
377380
// based on the platform source URL
378-
func installPlatformSourceResource(ctx context.Context, k8sClient client.Client, sourceURL, resourceName, refSpec string) error {
381+
func installPlatformSourceResource(ctx context.Context, k8sClient client.Client, sourceURL, resourceName, refSpec, secretName string) error {
379382
logger := log.FromContext(ctx)
380383

381384
// Parse the source URL to determine type
@@ -393,12 +396,12 @@ func installPlatformSourceResource(ctx context.Context, k8sClient client.Client,
393396
var obj client.Object
394397
switch sourceType {
395398
case "oci":
396-
obj, err = generateOCIRepository(resourceName, repoURL, refMap)
399+
obj, err = generateOCIRepository(resourceName, repoURL, refMap, secretName)
397400
if err != nil {
398401
return fmt.Errorf("failed to generate OCIRepository: %w", err)
399402
}
400403
case "git":
401-
obj, err = generateGitRepository(resourceName, repoURL, refMap)
404+
obj, err = generateGitRepository(resourceName, repoURL, refMap, secretName)
402405
if err != nil {
403406
return fmt.Errorf("failed to generate GitRepository: %w", err)
404407
}
@@ -558,7 +561,7 @@ func validateGitRef(refMap map[string]string) error {
558561
}
559562

560563
// generateOCIRepository creates an OCIRepository resource
561-
func generateOCIRepository(name, repoURL string, refMap map[string]string) (*sourcev1.OCIRepository, error) {
564+
func generateOCIRepository(name, repoURL string, refMap map[string]string, secretName string) (*sourcev1.OCIRepository, error) {
562565
if err := validateOCIRef(refMap); err != nil {
563566
return nil, err
564567
}
@@ -588,11 +591,18 @@ func generateOCIRepository(name, repoURL string, refMap map[string]string) (*sou
588591
}
589592
}
590593

594+
// Set secretRef if secret name is provided
595+
if secretName != "" {
596+
obj.Spec.SecretRef = &fluxmeta.LocalObjectReference{
597+
Name: secretName,
598+
}
599+
}
600+
591601
return obj, nil
592602
}
593603

594604
// generateGitRepository creates a GitRepository resource
595-
func generateGitRepository(name, repoURL string, refMap map[string]string) (*sourcev1.GitRepository, error) {
605+
func generateGitRepository(name, repoURL string, refMap map[string]string, secretName string) (*sourcev1.GitRepository, error) {
596606
if err := validateGitRef(refMap); err != nil {
597607
return nil, err
598608
}
@@ -623,6 +633,13 @@ func generateGitRepository(name, repoURL string, refMap map[string]string) (*sou
623633
}
624634
}
625635

636+
// Set secretRef if secret name is provided
637+
if secretName != "" {
638+
obj.Spec.SecretRef = &fluxmeta.LocalObjectReference{
639+
Name: secretName,
640+
}
641+
}
642+
626643
return obj, nil
627644
}
628645

‎cmd/cozystack-operator/main_test.go‎

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ func TestValidateGitRef(t *testing.T) {
487487

488488
func TestGenerateOCIRepository(t *testing.T) {
489489
refMap := map[string]string{"tag": "v1.0", "digest": "sha256:abc123"}
490-
obj, err := generateOCIRepository("my-repo", "oci://registry.example.com/repo", refMap)
490+
obj, err := generateOCIRepository("my-repo", "oci://registry.example.com/repo", refMap, "")
491491
if err != nil {
492492
t.Fatalf("unexpected error: %v", err)
493493
}
@@ -510,10 +510,13 @@ func TestGenerateOCIRepository(t *testing.T) {
510510
if obj.Spec.Reference.Digest != "sha256:abc123" {
511511
t.Errorf("expected digest %q, got %q", "sha256:abc123", obj.Spec.Reference.Digest)
512512
}
513+
if obj.Spec.SecretRef != nil {
514+
t.Errorf("expected SecretRef to be nil when secretName is empty, got %+v", obj.Spec.SecretRef)
515+
}
513516
}
514517

515518
func TestGenerateOCIRepository_NoRef(t *testing.T) {
516-
obj, err := generateOCIRepository("my-repo", "oci://registry.example.com/repo", map[string]string{})
519+
obj, err := generateOCIRepository("my-repo", "oci://registry.example.com/repo", map[string]string{}, "")
517520
if err != nil {
518521
t.Fatalf("unexpected error: %v", err)
519522
}
@@ -523,15 +526,28 @@ func TestGenerateOCIRepository_NoRef(t *testing.T) {
523526
}
524527

525528
func TestGenerateOCIRepository_InvalidRef(t *testing.T) {
526-
_, err := generateOCIRepository("my-repo", "oci://registry.example.com/repo", map[string]string{"branch": "main"})
529+
_, err := generateOCIRepository("my-repo", "oci://registry.example.com/repo", map[string]string{"branch": "main"}, "")
527530
if err == nil {
528531
t.Fatal("expected error for invalid OCI ref key, got nil")
529532
}
530533
}
531534

535+
func TestGenerateOCIRepository_WithSecret(t *testing.T) {
536+
obj, err := generateOCIRepository("my-repo", "oci://registry.example.com/repo", map[string]string{"tag": "v1.0"}, "my-registry-creds")
537+
if err != nil {
538+
t.Fatalf("unexpected error: %v", err)
539+
}
540+
if obj.Spec.SecretRef == nil {
541+
t.Fatal("expected SecretRef to be set when secretName is non-empty")
542+
}
543+
if obj.Spec.SecretRef.Name != "my-registry-creds" {
544+
t.Errorf("expected SecretRef.Name %q, got %q", "my-registry-creds", obj.Spec.SecretRef.Name)
545+
}
546+
}
547+
532548
func TestGenerateGitRepository(t *testing.T) {
533549
refMap := map[string]string{"branch": "main", "commit": "abc1234def5678"}
534-
obj, err := generateGitRepository("my-repo", "https://github.com/user/repo", refMap)
550+
obj, err := generateGitRepository("my-repo", "https://github.com/user/repo", refMap, "")
535551
if err != nil {
536552
t.Fatalf("unexpected error: %v", err)
537553
}
@@ -554,10 +570,13 @@ func TestGenerateGitRepository(t *testing.T) {
554570
if obj.Spec.Reference.Commit != "abc1234def5678" {
555571
t.Errorf("expected commit %q, got %q", "abc1234def5678", obj.Spec.Reference.Commit)
556572
}
573+
if obj.Spec.SecretRef != nil {
574+
t.Errorf("expected SecretRef to be nil when secretName is empty, got %+v", obj.Spec.SecretRef)
575+
}
557576
}
558577

559578
func TestGenerateGitRepository_NoRef(t *testing.T) {
560-
obj, err := generateGitRepository("my-repo", "https://github.com/user/repo", map[string]string{})
579+
obj, err := generateGitRepository("my-repo", "https://github.com/user/repo", map[string]string{}, "")
561580
if err != nil {
562581
t.Fatalf("unexpected error: %v", err)
563582
}
@@ -567,8 +586,21 @@ func TestGenerateGitRepository_NoRef(t *testing.T) {
567586
}
568587

569588
func TestGenerateGitRepository_InvalidRef(t *testing.T) {
570-
_, err := generateGitRepository("my-repo", "https://github.com/user/repo", map[string]string{"digest": "sha256:abc"})
589+
_, err := generateGitRepository("my-repo", "https://github.com/user/repo", map[string]string{"digest": "sha256:abc"}, "")
571590
if err == nil {
572591
t.Fatal("expected error for invalid Git ref key, got nil")
573592
}
574593
}
594+
595+
func TestGenerateGitRepository_WithSecret(t *testing.T) {
596+
obj, err := generateGitRepository("my-repo", "https://github.com/user/repo", map[string]string{"branch": "main"}, "my-git-creds")
597+
if err != nil {
598+
t.Fatalf("unexpected error: %v", err)
599+
}
600+
if obj.Spec.SecretRef == nil {
601+
t.Fatal("expected SecretRef to be set when secretName is non-empty")
602+
}
603+
if obj.Spec.SecretRef.Name != "my-git-creds" {
604+
t.Errorf("expected SecretRef.Name %q, got %q", "my-git-creds", obj.Spec.SecretRef.Name)
605+
}
606+
}

‎go.mod‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/cozystack/cozystack-scheduler/pkg/apis v0.1.1
1010
github.com/emicklei/dot v1.10.0
1111
github.com/fluxcd/helm-controller/api v1.4.3
12+
github.com/fluxcd/pkg/apis/meta v1.23.0
1213
github.com/fluxcd/source-controller/api v1.7.4
1314
github.com/fluxcd/source-watcher/api/v2 v2.0.3
1415
github.com/go-logr/logr v1.4.3
@@ -55,7 +56,6 @@ require (
5556
github.com/felixge/httpsnoop v1.0.4 // indirect
5657
github.com/fluxcd/pkg/apis/acl v0.9.0 // indirect
5758
github.com/fluxcd/pkg/apis/kustomize v1.13.0 // indirect
58-
github.com/fluxcd/pkg/apis/meta v1.23.0 // indirect
5959
github.com/fsnotify/fsnotify v1.9.0 // indirect
6060
github.com/fxamacker/cbor/v2 v2.9.0 // indirect
6161
github.com/go-logr/stdr v1.2.2 // indirect

‎packages/core/installer/templates/cozystack-operator.yaml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ spec:
7878
{{- if .Values.cozystackOperator.platformSourceRef }}
7979
- --platform-source-ref={{ .Values.cozystackOperator.platformSourceRef }}
8080
{{- end }}
81+
{{- if .Values.cozystackOperator.platformSourceSecret }}
82+
- --platform-source-secret={{ .Values.cozystackOperator.platformSourceSecret }}
83+
{{- end }}
8184
{{- if eq .Values.cozystackOperator.variant "talos" }}
8285
env:
8386
# Talos KubePrism endpoint

‎packages/core/installer/values.yaml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ cozystackOperator:
2929
# (operator default: 5). Number of release revisions Helm keeps per HR.
3030
# Use 0 for unlimited; lower values reduce Secret accumulation in test clusters.
3131
helmReleaseMaxHistory: ""
32+
# Name of the Secret in cozy-system namespace containing credentials for the
33+
# platform source. When non-empty, sets spec.secretRef on the generated
34+
# OCIRepository or GitRepository resource. Secret type depends on source kind:
35+
# OCI (oci://...): kubernetes.io/dockerconfigjson with registry credentials.
36+
# Git over HTTPS: Opaque secret with `username`+`password`, or `bearerToken`.
37+
# Git over SSH: Opaque secret with `identity` (PEM private key) and `known_hosts`.
38+
# The Secret must already exist in cozy-system before the operator reconciles;
39+
# otherwise source-controller will report a failed condition until it is created.
40+
# See FluxCD source-controller docs for the exact key layout per scheme.
41+
platformSourceSecret: ''
3242
# Generic variant configuration (only used when cozystackOperator.variant=generic)
3343
cozystack:
3444
# Kubernetes API server host (IP only, no protocol/port)

0 commit comments

Comments
 (0)