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

feat: add funcs for check #253

Merged
merged 3 commits into from
Aug 24, 2022
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
6 changes: 3 additions & 3 deletions apis/data/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ limitations under the License.
// Package v1alpha1 contains API Schema definitions for the types v1alpha1 API group
// this group is a duck type only package that hosts common objects and interfaces in katanomi
// very similar to knative.dev/pkg/apis/duck/v1 or k8s.io/apimachinery/pkg/apis/meta/v1
//+kubebuilder:object:generate=true
//+k8s:deepcopy-gen=package
//+groupName=data.katanomi.dev
// +kubebuilder:object:generate=true
// +k8s:deepcopy-gen=package
// +groupName=data.katanomi.dev
package v1alpha1

import (
Expand Down
2 changes: 1 addition & 1 deletion apis/data/v1alpha1/zz_generated.deepcopy.go

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

37 changes: 37 additions & 0 deletions apis/meta/v1alpha1/approve_funcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Copyright 2022 The Katanomi 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.
*/

package v1alpha1

// HasApprover indicates whether the approver exists.
func (a *Approval) HasApprover() bool {
return a != nil && len(a.Users) != 0
}

// HasApprover indicates whether the approver exists.
func (a *ApprovalSpec) HasApprover() bool {
return a != nil && len(a.Users) != 0
}

// IsEnabled indicates whether the approval is enabled.
func (a *ApprovalSpec) IsEnabled() bool {
return a != nil && len(a.Users) != 0
}

// TimeoutEnabled indicates whether the timeout is enabled.
func (a *ApprovalSpec) TimeoutEnabled() bool {
return a != nil && a.Timeout.Duration > 0
}
84 changes: 84 additions & 0 deletions apis/meta/v1alpha1/approve_funcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2021 The Katanomi 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.
*/

package v1alpha1

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = Describe("Test.Approval", func() {
DescribeTable("Approval.HasApprover",
func(input *Approval, expected bool) {
actual := input.HasApprover()
Expect(actual).To(Equal(expected))
},
Entry("nil pointer",
nil,
false,
),
Entry("no user",
&Approval{},
false,
),
Entry("has user",
&Approval{
Users: []UserApproval{{
Subject: rbacv1.Subject{
Name: "user1",
},
}},
},
true,
),
)
})

var _ = Describe("Test.ApprovalSpec", func() {
DescribeTable("ApprovalSpec.HasApprover.IsEnabled.TimeoutEnabled",
func(input *ApprovalSpec, expected bool) {
var actual bool
actual = input.HasApprover()
Expect(actual).To(Equal(expected))
actual = input.IsEnabled()
Expect(actual).To(Equal(expected))
actual = input.TimeoutEnabled()
Expect(actual).To(Equal(expected))
},
Entry("nil pointer",
nil,
false,
),
Entry("no user",
&ApprovalSpec{},
false,
),
Entry("has user",
&ApprovalSpec{
Users: []rbacv1.Subject{{
Name: "user1",
}},
Timeout: metav1.Duration{
Duration: 1,
},
},
true,
),
)
})
File renamed without changes.
83 changes: 83 additions & 0 deletions apis/meta/v1alpha1/check_funcs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
Copyright 2022 The Katanomi 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.
*/

package v1alpha1

import (
"time"

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

// GetApprovalUsers get the approval users
func (c *Check) GetApprovalUsers() UserApprovals {
if !c.HasApprover() {
return nil
}
return c.Approval.Users
}

// HasApprover indicates whether the approver exists.
func (c *Check) HasApprover() bool {
return c != nil && c.Approval.HasApprover()
}

// IsNotSet Indicates that no approver is set.
func (c *Check) IsNotSet() bool {
return c == nil || c.Approval == nil || len(c.Approval.Users) == 0
}

// ApprovalIsStarted indicates whether the approver is started.
func (cs *ApprovalCheckStatus) ApprovalIsStarted() bool {
return cs != nil && !cs.ApprovalStartTime.IsZero()
}

// StartWaitingApproval set the approval start time, if it is empty.
func (cs *ApprovalCheckStatus) StartWaitingApproval() {
if cs == nil {
return
}
if cs.ApprovalStartTime.IsZero() {
cs.ApprovalStartTime = &metav1.Time{Time: time.Now()}
}
}

func (users UserApprovals) GetBySubject(subject rbacv1.Subject) *UserApproval {
if users == nil {
return nil
}
for i := range users {
user := &users[i]
if user.Subject == subject {
return user
}
}
return nil
}

func (as ApprovalStatuses) GetBySubject(subject rbacv1.Subject) *ApprovalStatus {
if as == nil {
return nil
}
for i := range as {
user := &as[i]
if user.Subject == subject {
return user
}
}
return nil
}
85 changes: 85 additions & 0 deletions apis/meta/v1alpha1/check_funcs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2022 The Katanomi 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.
*/

package v1alpha1

import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
rbacv1 "k8s.io/api/rbac/v1"
)

var _ = Describe("Test.Check", func() {
DescribeTable("Check.GetApprovalUsers",
func(input *Approval, expected UserApprovals) {
check := &Check{
Approval: input,
}
actual := check.GetApprovalUsers()
Expect(actual).To(Equal(expected))
},
Entry("nil pointer",
nil,
nil,
),
Entry("no user",
&Approval{},
nil,
),
Entry("has user",
&Approval{
Users: []UserApproval{{
Subject: rbacv1.Subject{
Name: "user1",
},
}},
},
[]UserApproval{{
Subject: rbacv1.Subject{
Name: "user1",
},
}},
),
)

DescribeTable("Check.IsNotSet",
func(input *Approval, expected bool) {
check := &Check{
Approval: input,
}
actual := check.IsNotSet()
Expect(actual).To(Equal(expected))
},
Entry("nil pointer",
nil,
true,
),
Entry("no user",
&Approval{},
true,
),
Entry("has user",
&Approval{
Users: []UserApproval{{
Subject: rbacv1.Subject{
Name: "user1",
},
}},
},
false,
),
)
})
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions apis/meta/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ limitations under the License.
// Package v1alpha1 contains API Schema definitions for the types v1alpha1 API group
// this group is a duck type only package that hosts common objects and interfaces in katanomi
// very similar to knative.dev/pkg/apis/duck/v1 or k8s.io/apimachinery/pkg/apis/meta/v1
//+kubebuilder:object:generate=true
//+k8s:deepcopy-gen=package
//+groupName=meta.katanomi.dev
// +kubebuilder:object:generate=true
// +k8s:deepcopy-gen=package
// +groupName=meta.katanomi.dev
package v1alpha1

import (
Expand Down
13 changes: 7 additions & 6 deletions apis/meta/v1alpha1/triggeredby_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,13 @@ type TriggeredBy struct {
}

// IsZero basic function returns true when all attributes of the object are empty
func (by TriggeredBy) IsZero() bool {
return by.User == nil &&
by.CloudEvent == nil &&
by.Ref == nil &&
by.TriggeredTimestamp.IsZero() &&
by.TriggeredType.String() == ""
func (by *TriggeredBy) IsZero() bool {
return (by == nil) ||
(by.User == nil &&
by.CloudEvent == nil &&
by.Ref == nil &&
by.TriggeredTimestamp.IsZero() &&
by.TriggeredType.String() == "")
}

// FromAnnotation will set `by` from annotations
Expand Down
Loading