-
Notifications
You must be signed in to change notification settings - Fork 0
/
organization.go
85 lines (69 loc) · 2.26 KB
/
organization.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package organization
import (
"fmt"
"os"
"strings"
templateorg "github.com/giantswarm/kubectl-gs/v2/pkg/template/organization"
orgv1alpha1 "github.com/giantswarm/organization-operator/api/v1alpha1"
"k8s.io/client-go/kubernetes/scheme"
"github.com/giantswarm/clustertest/pkg/utils"
)
func init() {
_ = orgv1alpha1.AddToScheme(scheme.Scheme)
}
const (
// DeleteAnnotation is added to Organizations created during testing.
// This is to ensure only those with this annotation can be deleted to avoid accidentally deleting a shared Org.
DeleteAnnotation = "e2e-test-cleanup"
)
// Org contains details about an Organization
type Org struct {
Name string
namespace string
}
// NewRandomOrg returns an Org with a randomly generated name
func NewRandomOrg() *Org {
return New(utils.GenerateRandomName("t"))
}
// New returns a new Org with the provided name
func New(name string) *Org {
return &Org{
Name: name,
namespace: fmt.Sprintf("org-%s", name),
}
}
// NewFromNamespace returns a new Org, taking the name from the passed namespace.
func NewFromNamespace(namespace string) *Org {
return &Org{
Name: strings.TrimPrefix(namespace, "org-"),
namespace: namespace,
}
}
// GetNamespace returns the associated namespace for the Organization
func (o *Org) GetNamespace() string {
return o.namespace
}
// Build generates the Organization CR for applying to the cluster
func (o *Org) Build() (*orgv1alpha1.Organization, error) {
orgCR, err := templateorg.NewOrganizationCR(templateorg.Config{
Name: o.Name,
})
if err != nil {
return nil, err
}
// We want to add an annotation to track if we should be removing the org when done or not
// This check will allow us to re-use existing orgs too without accidentally deleting the org when done
orgCR.ObjectMeta.Annotations = map[string]string{
DeleteAnnotation: "true",
}
// If found, populate details about Tekton run as labels
orgCR.ObjectMeta.Labels = map[string]string{}
if os.Getenv("TEKTON_PIPELINE_RUN") != "" {
orgCR.ObjectMeta.Labels["cicd.giantswarm.io/pipelinerun"] = os.Getenv("TEKTON_PIPELINE_RUN")
}
if os.Getenv("TEKTON_TASK_RUN") != "" {
orgCR.ObjectMeta.Labels["cicd.giantswarm.io/taskrun"] = os.Getenv("TEKTON_TASK_RUN")
}
orgCR.Status.Namespace = o.GetNamespace()
return orgCR, err
}