Skip to content

Commit

Permalink
feat(RHTAPREL-278): add match info to releasePlans and RPAs
Browse files Browse the repository at this point in the history
This commit adds a ReleasePlanAdmission controller and modifies the
ReleasePlan controller. The controllers perform similar actions - the
update their respective resource's Status to say whether or not it is
matched to its paired resource.

Signed-off-by: Johnny Bieren <[email protected]>
  • Loading branch information
johnbieren committed Nov 29, 2023
1 parent e6277a2 commit 06fa65f
Show file tree
Hide file tree
Showing 33 changed files with 2,372 additions and 75 deletions.
1 change: 1 addition & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ resources:
- api:
crdVersion: v1
namespaced: true
controller: true
domain: redhat.com
group: appstudio
kind: ReleasePlanAdmission
Expand Down
14 changes: 14 additions & 0 deletions api/v1alpha1/match_conditions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package v1alpha1

import "github.com/redhat-appstudio/operator-toolkit/conditions"

const (
// matchedConditionType is the type used to track the status of the ReleasePlan being matched to a
// ReleasePlanAdmission or vice versa
MatchedConditionType conditions.ConditionType = "Matched"
)

const (
// MatchedReason is the reason set when a resource is matched
MatchedReason conditions.ConditionReason = "Matched"
)
2 changes: 1 addition & 1 deletion api/v1alpha1/release_types_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2022.
Copyright 2023.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
56 changes: 53 additions & 3 deletions api/v1alpha1/releaseplan_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ limitations under the License.
package v1alpha1

import (
"fmt"

"github.com/redhat-appstudio/operator-toolkit/conditions"
"github.com/redhat-appstudio/release-service/metadata"
tektonutils "github.com/redhat-appstudio/release-service/tekton/utils"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
)

// ReleasePlanSpec defines the desired state of ReleasePlan.
Expand Down Expand Up @@ -51,17 +57,29 @@ type ReleasePlanSpec struct {
Target string `json:"target"`
}

// MatchedReleasePlanAdmission defines the relevant information for a matched ReleasePlanAdmission.
type MatchedReleasePlanAdmission struct {
// Name contains the namespaced name of the releasePlanAdmission
// +kubebuilder:validation:Pattern=^[a-z0-9]([-a-z0-9]*[a-z0-9])?\/[a-z0-9]([-a-z0-9]*[a-z0-9])?$
// +optional
Name string `json:"name,omitempty"`

// Active indicates whether the ReleasePlanAdmission is set to auto-release or not
// +kubebuilder:default:false
// +optional
Active bool `json:"active,omitempty"`
}

// ReleasePlanStatus defines the observed state of ReleasePlan.
type ReleasePlanStatus struct {
// Conditions represent the latest available observations for the releasePlan
// +optional
Conditions []metav1.Condition `json:"conditions"`

// ReleasePlanAdmission contains the namespaced name of the releasePlanAdmission this ReleasePlan is
// ReleasePlanAdmission contains the information of the releasePlanAdmission this ReleasePlan is
// matched to
// +kubebuilder:validation:Pattern=^[a-z0-9]([-a-z0-9]*[a-z0-9])?\/[a-z0-9]([-a-z0-9]*[a-z0-9])?$
// +optional
ReleasePlanAdmission string `json:"releasePlanAdmission,omitempty"`
ReleasePlanAdmission MatchedReleasePlanAdmission `json:"releasePlanAdmission,omitempty"`
}

// +kubebuilder:object:root=true
Expand All @@ -79,6 +97,38 @@ type ReleasePlan struct {
Status ReleasePlanStatus `json:"status,omitempty"`
}

// IsMatched checks whether the ReleasePlan is matched to a ReleasePlanAdmission.
func (rp *ReleasePlan) IsMatched() bool {
return meta.IsStatusConditionTrue(rp.Status.Conditions, MatchedConditionType.String())
}

// MarkMatched marks the ReleasePlan as matched to a given ReleasePlanAdmission.
func (rp *ReleasePlan) MarkMatched(releasePlanAdmission *ReleasePlanAdmission) {
rp.setMatchedStatus(releasePlanAdmission, metav1.ConditionTrue)
}

// MarkUnmatched marks the ReleasePlan as not matched to any ReleasePlanAdmission.
func (rp *ReleasePlan) MarkUnmatched() {
if !rp.IsMatched() {
return
}

rp.setMatchedStatus(nil, metav1.ConditionFalse)
}

// setMatchedStatus sets the ReleasePlan Matched condition based on the passed releasePlanAdmission and status.
func (rp *ReleasePlan) setMatchedStatus(releasePlanAdmission *ReleasePlanAdmission, status metav1.ConditionStatus) {
rp.Status.ReleasePlanAdmission = MatchedReleasePlanAdmission{}

if releasePlanAdmission != nil {
rp.Status.ReleasePlanAdmission.Name = fmt.Sprintf("%s%c%s", releasePlanAdmission.GetNamespace(),
types.Separator, releasePlanAdmission.GetName())
rp.Status.ReleasePlanAdmission.Active = (releasePlanAdmission.GetLabels()[metadata.AutoReleaseLabel] == "true")
}

conditions.SetCondition(&rp.Status.Conditions, MatchedConditionType, status, MatchedReason)
}

// +kubebuilder:object:root=true

// ReleasePlanList contains a list of ReleasePlan.
Expand Down
143 changes: 143 additions & 0 deletions api/v1alpha1/releaseplan_types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
Copyright 2022.
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.
*/

package v1alpha1

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/redhat-appstudio/operator-toolkit/conditions"
"github.com/redhat-appstudio/release-service/metadata"

"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = Describe("ReleasePlan type", func() {
When("IsMatched method is called", func() {
var releasePlan *ReleasePlan

BeforeEach(func() {
releasePlan = &ReleasePlan{}
})

It("should return true when the matched condition status is True", func() {
conditions.SetCondition(&releasePlan.Status.Conditions, MatchedConditionType, metav1.ConditionTrue, "")
Expect(releasePlan.IsMatched()).To(BeTrue())
})

It("should return false when the matched condition status is False", func() {
conditions.SetCondition(&releasePlan.Status.Conditions, MatchedConditionType, metav1.ConditionFalse, "")
Expect(releasePlan.IsMatched()).To(BeFalse())
})

It("should return false when the matched condition status is Unknown", func() {
conditions.SetCondition(&releasePlan.Status.Conditions, MatchedConditionType, metav1.ConditionUnknown, "")
Expect(releasePlan.IsMatched()).To(BeFalse())
})

It("should return false when the matched condition is missing", func() {
Expect(releasePlan.IsMatched()).To(BeFalse())
})
})

When("MarkMatched method is called", func() {
var releasePlan *ReleasePlan
var releasePlanAdmission *ReleasePlanAdmission

BeforeEach(func() {
releasePlan = &ReleasePlan{}
releasePlanAdmission = &ReleasePlanAdmission{
ObjectMeta: metav1.ObjectMeta{
Name: "rpa",
Namespace: "default",
Labels: map[string]string{
metadata.AutoReleaseLabel: "false",
},
},
}
})

It("should mark the ReleasePlan as matched", func() {
releasePlan.MarkMatched(releasePlanAdmission)
Expect(releasePlan.Status.ReleasePlanAdmission.Name).To(Equal("default/rpa"))
Expect(releasePlan.Status.ReleasePlanAdmission.Active).To(BeFalse())
condition := meta.FindStatusCondition(releasePlan.Status.Conditions, MatchedConditionType.String())
Expect(condition).NotTo(BeNil())
Expect(condition.Status).To(Equal(metav1.ConditionTrue))
})
})

When("MarkUnmatched method is called", func() {
var releasePlan *ReleasePlan
var releasePlanAdmission *ReleasePlanAdmission

BeforeEach(func() {
releasePlan = &ReleasePlan{}
releasePlanAdmission = &ReleasePlanAdmission{
ObjectMeta: metav1.ObjectMeta{
Name: "rpa",
Namespace: "default",
Labels: map[string]string{
metadata.AutoReleaseLabel: "false",
},
},
}
})

It("should do nothing if the ReleasePlan is not matched", func() {
releasePlan.setMatchedStatus(releasePlanAdmission, metav1.ConditionFalse) // IsMatched relies on the condition, not value of RPA
releasePlan.MarkUnmatched()
Expect(releasePlan.Status.ReleasePlanAdmission.Name).To(Equal("default/rpa"))
})

It("should mark the ReleasePlan as unmatched", func() {
releasePlan.MarkMatched(releasePlanAdmission)
releasePlan.MarkUnmatched()
Expect(releasePlan.Status.ReleasePlanAdmission).To(Equal(MatchedReleasePlanAdmission{}))
condition := meta.FindStatusCondition(releasePlan.Status.Conditions, MatchedConditionType.String())
Expect(condition).NotTo(BeNil())
Expect(condition.Status).To(Equal(metav1.ConditionFalse))
})
})

When("setMatchedStatus method is called", func() {
var releasePlan *ReleasePlan
var releasePlanAdmission *ReleasePlanAdmission

BeforeEach(func() {
releasePlan = &ReleasePlan{}
releasePlanAdmission = &ReleasePlanAdmission{
ObjectMeta: metav1.ObjectMeta{
Name: "rpa",
Namespace: "default",
Labels: map[string]string{
metadata.AutoReleaseLabel: "true",
},
},
}
})

It("should set the ReleasePlanAdmission and matched condition", func() {
releasePlan.setMatchedStatus(releasePlanAdmission, metav1.ConditionUnknown)
Expect(releasePlan.Status.ReleasePlanAdmission.Name).To(Equal("default/rpa"))
Expect(releasePlan.Status.ReleasePlanAdmission.Active).To(BeTrue())
condition := meta.FindStatusCondition(releasePlan.Status.Conditions, MatchedConditionType.String())
Expect(condition).NotTo(BeNil())
Expect(condition.Status).To(Equal(metav1.ConditionUnknown))
})
})
})
45 changes: 43 additions & 2 deletions api/v1alpha1/releaseplanadmission_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ limitations under the License.
package v1alpha1

import (
"fmt"
"sort"

"github.com/redhat-appstudio/operator-toolkit/conditions"
"github.com/redhat-appstudio/release-service/metadata"
tektonutils "github.com/redhat-appstudio/release-service/tekton/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
)

// ReleasePlanAdmissionSpec defines the desired state of ReleasePlanAdmission.
Expand Down Expand Up @@ -59,15 +65,28 @@ type ReleasePlanAdmissionSpec struct {
ServiceAccount string `json:"serviceAccount,omitempty"`
}

// MatchedReleasePlan defines the relevant information for a matched ReleasePlan.
type MatchedReleasePlan struct {
// Name contains the namespaced name of the ReleasePlan
// +kubebuilder:validation:Pattern=^[a-z0-9]([-a-z0-9]*[a-z0-9])?\/[a-z0-9]([-a-z0-9]*[a-z0-9])?$
// +optional
Name string `json:"name,omitempty"`

// Active indicates whether the ReleasePlan is set to auto-release or not
// +kubebuilder:default:false
// +optional
Active bool `json:"active,omitempty"`
}

// ReleasePlanAdmissionStatus defines the observed state of ReleasePlanAdmission.
type ReleasePlanAdmissionStatus struct {
// Conditions represent the latest available observations for the releasePlanAdmission
// +optional
Conditions []metav1.Condition `json:"conditions"`

// ReleasePlan is a list of references to releasePlans matched to the ReleasePlanAdmission
// ReleasePlan is a list of releasePlans matched to the ReleasePlanAdmission
// +optional
ReleasePlans []string `json:"releasePlans"`
ReleasePlans []MatchedReleasePlan `json:"releasePlans"`
}

// +kubebuilder:object:root=true
Expand All @@ -85,6 +104,28 @@ type ReleasePlanAdmission struct {
Status ReleasePlanAdmissionStatus `json:"status,omitempty"`
}

// ClearMatchingInfo marks the ReleasePlanAdmission as no longer matched to any ReleasePlan.
func (rpa *ReleasePlanAdmission) ClearMatchingInfo() {
rpa.Status.ReleasePlans = []MatchedReleasePlan{}
conditions.SetCondition(&rpa.Status.Conditions, MatchedConditionType, metav1.ConditionFalse, MatchedReason)
}

// MarkMatched marks the ReleasePlanAdmission as matched to a given ReleasePlan.
func (rpa *ReleasePlanAdmission) MarkMatched(releasePlan *ReleasePlan) {
pairedReleasePlan := MatchedReleasePlan{
Name: fmt.Sprintf("%s%c%s", releasePlan.GetNamespace(), types.Separator, releasePlan.GetName()),
Active: (releasePlan.GetLabels()[metadata.AutoReleaseLabel] == "true"),
}

rpa.Status.ReleasePlans = append(rpa.Status.ReleasePlans, pairedReleasePlan)
sort.Slice(rpa.Status.ReleasePlans, func(i, j int) bool {
return rpa.Status.ReleasePlans[i].Name < rpa.Status.ReleasePlans[j].Name
})

// Update the condition every time one is added so lastTransitionTime updates
conditions.SetCondition(&rpa.Status.Conditions, MatchedConditionType, metav1.ConditionTrue, MatchedReason)
}

// +kubebuilder:object:root=true

// ReleasePlanAdmissionList contains a list of ReleasePlanAdmission.
Expand Down
Loading

0 comments on commit 06fa65f

Please sign in to comment.