Skip to content
Open
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
16 changes: 8 additions & 8 deletions pkg/apis/operator/v1alpha1/tektonconfig_default_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ func Test_SetDefaults_OpenShift_MigratesKubernetesPipelinesAsCode(t *testing.T)
Spec: TektonConfigSpec{
CommonSpec: CommonSpec{TargetNamespace: "ns"},
Platforms: Platforms{
Kubernetes: Kubernetes{PipelinesAsCode: kpac},
Kubernetes: &Kubernetes{PipelinesAsCode: kpac},
},
},
}
tc.SetDefaults(context.TODO())
if tc.Spec.Platforms.Kubernetes.PipelinesAsCode != nil {
t.Fatalf("expected kubernetes.pipelinesAsCode cleared after migration, got %+v", tc.Spec.Platforms.Kubernetes.PipelinesAsCode)
if tc.Spec.Platforms.Kubernetes != nil {
t.Fatalf("expected kubernetes platform nil after migration, got %+v", tc.Spec.Platforms.Kubernetes)
}
if tc.Spec.Platforms.OpenShift.PipelinesAsCode == nil || tc.Spec.Platforms.OpenShift.PipelinesAsCode.PACSettings.Settings["application-name"] != "test" {
t.Fatalf("expected PAC migrated to openshift, got %+v", tc.Spec.Platforms.OpenShift.PipelinesAsCode)
if tc.Spec.Platforms.OpenShift == nil || tc.Spec.Platforms.OpenShift.PipelinesAsCode == nil || tc.Spec.Platforms.OpenShift.PipelinesAsCode.PACSettings.Settings["application-name"] != "test" {
t.Fatalf("expected PAC migrated to openshift, got %+v", tc.Spec.Platforms.OpenShift)
}
}

Expand Down Expand Up @@ -211,7 +211,7 @@ func Test_SetDefaults_PipelineAsCode(t *testing.T) {
Spec: TektonConfigSpec{
Addon: Addon{EnablePAC: ptr.Bool(false)},
Platforms: Platforms{
OpenShift: OpenShift{
OpenShift: &OpenShift{
PipelinesAsCode: &PipelinesAsCode{Enable: ptr.Bool(true)},
},
},
Expand All @@ -227,7 +227,7 @@ func Test_SetDefaults_PipelineAsCode(t *testing.T) {
Spec: TektonConfigSpec{
Addon: Addon{EnablePAC: ptr.Bool(false)},
Platforms: Platforms{
Kubernetes: Kubernetes{
Kubernetes: &Kubernetes{
PipelinesAsCode: &PipelinesAsCode{Enable: ptr.Bool(true)},
},
},
Expand Down Expand Up @@ -319,7 +319,7 @@ func Test_SetDefaults_SCC(t *testing.T) {
tektonConfig := TektonConfig{
Spec: TektonConfigSpec{
Platforms: Platforms{
OpenShift: OpenShift{
OpenShift: &OpenShift{
SCC: test.inputSCC,
},
},
Expand Down
13 changes: 11 additions & 2 deletions pkg/apis/operator/v1alpha1/tektonconfig_defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,20 @@ func (tc *TektonConfig) SetDefaults(ctx context.Context) {
tc.Spec.Scheduler.SetDefaults()

if IsOpenShiftPlatform() {
if tc.Spec.Platforms.OpenShift == nil {
tc.Spec.Platforms.OpenShift = &OpenShift{}
}

// PAC may appear under spec.platforms.kubernetes if the mutating webhook ran without
// PLATFORM=openshift (e.g. wrong image/order) or from older releases. Move it to
// spec.platforms.openshift so the stored TektonConfig matches the OpenShift operator.
if tc.Spec.Platforms.Kubernetes.PipelinesAsCode != nil {
if tc.Spec.Platforms.Kubernetes != nil && tc.Spec.Platforms.Kubernetes.PipelinesAsCode != nil {
if tc.Spec.Platforms.OpenShift.PipelinesAsCode == nil {
p := *tc.Spec.Platforms.Kubernetes.PipelinesAsCode
tc.Spec.Platforms.OpenShift.PipelinesAsCode = &p
}
tc.Spec.Platforms.Kubernetes.PipelinesAsCode = nil
}
tc.Spec.Platforms.Kubernetes = nil

if tc.Spec.Platforms.OpenShift.PipelinesAsCode != nil {
tc.Spec.Addon.EnablePAC = nil
Expand Down Expand Up @@ -88,6 +92,11 @@ func (tc *TektonConfig) SetDefaults(ctx context.Context) {
setAddonDefaults(&tc.Spec.Addon)
} else {
// Kubernetes Platform
if tc.Spec.Platforms.Kubernetes == nil {
tc.Spec.Platforms.Kubernetes = &Kubernetes{}
}
tc.Spec.Platforms.OpenShift = nil

if tc.Spec.Platforms.Kubernetes.PipelinesAsCode == nil {
tc.Spec.Platforms.Kubernetes.PipelinesAsCode = &PipelinesAsCode{
Enable: ptr.Bool(true),
Expand Down
14 changes: 10 additions & 4 deletions pkg/apis/operator/v1alpha1/tektonconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,15 @@ type TektonConfigSpec struct {
// platform's spec.platforms subtree.
func (s *TektonConfigSpec) PipelinesAsCodeForCurrentPlatform() *PipelinesAsCode {
if IsOpenShiftPlatform() {
return s.Platforms.OpenShift.PipelinesAsCode
if s.Platforms.OpenShift != nil {
return s.Platforms.OpenShift.PipelinesAsCode
}
return nil
}
return s.Platforms.Kubernetes.PipelinesAsCode
if s.Platforms.Kubernetes != nil {
return s.Platforms.Kubernetes.PipelinesAsCode
}
return nil
}

// TektonConfigStatus defines the observed state of TektonConfig
Expand Down Expand Up @@ -215,10 +221,10 @@ type Config struct {
type Platforms struct {
// OpenShift allows configuring openshift specific components and configurations
// +optional
OpenShift OpenShift `json:"openshift,omitempty"`
OpenShift *OpenShift `json:"openshift,omitempty"`
// Kubernetes allows configuring kubernetes specific components and configurations
// +optional
Kubernetes Kubernetes `json:"kubernetes,omitempty"`
Kubernetes *Kubernetes `json:"kubernetes,omitempty"`
}

type Hub struct {
Expand Down
42 changes: 42 additions & 0 deletions pkg/apis/operator/v1alpha1/tektonconfig_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package v1alpha1

import (
"context"
"encoding/json"
"strings"
"testing"

"gotest.tools/v3/assert"
Expand Down Expand Up @@ -47,3 +49,43 @@ func TestPrune_PrunePerResourceJSONRoundTrip(t *testing.T) {
_, present := roundTripped["prune-per-resource"]
assert.Assert(t, present, "prune-per-resource key was dropped from marshaled JSON when false: %s", string(out))
}

func TestPlatforms_OmitEmptyOppositeplatform(t *testing.T) {
tests := []struct {
name string
platform string
absentKey string
presentKey string
}{
{
name: "OpenShift should omit kubernetes from serialized JSON",
platform: "openshift",
absentKey: "kubernetes",
presentKey: "openshift",
},
{
name: "Kubernetes should omit openshift from serialized JSON",
platform: "kubernetes",
absentKey: "openshift",
presentKey: "kubernetes",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("PLATFORM", tt.platform)

tc := &TektonConfig{}
tc.SetDefaults(context.TODO())

out, err := json.Marshal(tc.Spec.Platforms)
assert.NilError(t, err)

serialized := string(out)
assert.Assert(t, !strings.Contains(serialized, tt.absentKey),
"expected %q to be absent from serialized platforms JSON, got: %s", tt.absentKey, serialized)
assert.Assert(t, strings.Contains(serialized, tt.presentKey),
"expected %q to be present in serialized platforms JSON, got: %s", tt.presentKey, serialized)
})
}
}
14 changes: 7 additions & 7 deletions pkg/apis/operator/v1alpha1/tektonconfig_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ func (tc *TektonConfig) Validate(ctx context.Context) (errs *apis.FieldError) {
}

logger := logging.FromContext(ctx)
if IsOpenShiftPlatform() && tc.Spec.Platforms.OpenShift.PipelinesAsCode != nil {
if IsOpenShiftPlatform() && tc.Spec.Platforms.OpenShift != nil && tc.Spec.Platforms.OpenShift.PipelinesAsCode != nil {
errs = errs.Also(tc.Spec.Platforms.OpenShift.PipelinesAsCode.PACSettings.validate(logger, "spec.platforms.openshift.pipelinesAsCode"))
} else if !IsOpenShiftPlatform() && tc.Spec.Platforms.Kubernetes.PipelinesAsCode != nil {
} else if !IsOpenShiftPlatform() && tc.Spec.Platforms.Kubernetes != nil && tc.Spec.Platforms.Kubernetes.PipelinesAsCode != nil {
errs = errs.Also(tc.Spec.Platforms.Kubernetes.PipelinesAsCode.PACSettings.validate(logger, "spec.platforms.kubernetes.pipelinesAsCode"))
}

// validate SCC config
if IsOpenShiftPlatform() && tc.Spec.Platforms.OpenShift.SCC != nil {
if IsOpenShiftPlatform() && tc.Spec.Platforms.OpenShift != nil && tc.Spec.Platforms.OpenShift.SCC != nil {
defaultSCC := PipelinesSCC
if tc.Spec.Platforms.OpenShift.SCC.Default != "" {
defaultSCC = tc.Spec.Platforms.OpenShift.SCC.Default
Expand Down Expand Up @@ -195,12 +195,12 @@ func isValueInArray(arr []string, key string) bool {
return false
}

func isOpenShiftPlatformsSectionSet(o OpenShift) bool {
return o.PipelinesAsCode != nil || o.SCC != nil
func isOpenShiftPlatformsSectionSet(o *OpenShift) bool {
return o != nil && (o.PipelinesAsCode != nil || o.SCC != nil)
}

func isKubernetesPlatformsSectionSet(k Kubernetes) bool {
return k.PipelinesAsCode != nil
func isKubernetesPlatformsSectionSet(k *Kubernetes) bool {
return k != nil && k.PipelinesAsCode != nil
}

func verifySCCExists(ctx context.Context, sccName string) error {
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/operator/v1alpha1/tektonconfig_validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func Test_ValidateTektonConfig_OpenShiftPlatformsOnKubernetes(t *testing.T) {
},
Pruner: Prune{Disabled: true},
Platforms: Platforms{
OpenShift: OpenShift{
OpenShift: &OpenShift{
PipelinesAsCode: &PipelinesAsCode{Enable: ptr.Bool(true)},
},
},
Expand All @@ -120,7 +120,7 @@ func Test_ValidateTektonConfig_KubernetesPlatformsOnOpenShift(t *testing.T) {
},
Pruner: Prune{Disabled: true},
Platforms: Platforms{
Kubernetes: Kubernetes{
Kubernetes: &Kubernetes{
PipelinesAsCode: &PipelinesAsCode{Enable: ptr.Bool(true)},
},
},
Expand Down
12 changes: 10 additions & 2 deletions pkg/apis/operator/v1alpha1/zz_generated.deepcopy.go

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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright 2024 The Tekton Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
---
apiVersion: v1
kind: Namespace
metadata:
name: pipelines-as-code
labels:
app.kubernetes.io/version: "0.1.0"
app.kubernetes.io/instance: default
app.kubernetes.io/part-of: pipelines-as-code
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: pipelines-as-code-controller
namespace: pipelines-as-code
labels:
app.kubernetes.io/version: "0.1.0"
app.kubernetes.io/component: controller
app.kubernetes.io/instance: default
app.kubernetes.io/part-of: pipelines-as-code
---
apiVersion: v1
kind: ConfigMap
metadata:
name: pipelines-as-code
namespace: pipelines-as-code
labels:
app.kubernetes.io/version: "0.1.0"
app.kubernetes.io/part-of: pipelines-as-code
data:
application-name: "Pipelines as Code CI"
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: pipelines-as-code-controller
namespace: pipelines-as-code
labels:
app.kubernetes.io/version: "0.1.0"
app.kubernetes.io/component: controller
app.kubernetes.io/instance: default
app.kubernetes.io/part-of: pipelines-as-code
spec:
replicas: 1
selector:
matchLabels:
app.kubernetes.io/component: controller
app.kubernetes.io/instance: default
app.kubernetes.io/part-of: pipelines-as-code
template:
metadata:
labels:
app.kubernetes.io/version: "0.1.0"
app.kubernetes.io/component: controller
app.kubernetes.io/instance: default
app.kubernetes.io/part-of: pipelines-as-code
spec:
serviceAccountName: pipelines-as-code-controller
containers:
- name: pac-controller
image: ghcr.io/openshift-pipelines/pipelines-as-code-controller:0.1.0
ports:
- name: api
containerPort: 8080
securityContext:
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
runAsNonRoot: true
capabilities:
drop:
- "ALL"
seccompProfile:
type: RuntimeDefault

This file was deleted.

Loading
Loading