Skip to content

Commit 061b391

Browse files
committed
Add iampolicy set ability
1 parent 2c2d167 commit 061b391

File tree

6 files changed

+57
-17
lines changed

6 files changed

+57
-17
lines changed

pkg/apis/cloudruncontroller/v1alpha1/service_types.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ type ServiceSpec struct {
1515
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
1616
// Important: Run "operator-sdk generate k8s" to regenerate code after modifying this file
1717
// Add custom validation using kubebuilder tags: https://book.kubebuilder.io/beyond_basics/generating_crd.html
18-
Project string `json:"project"`
19-
Service run.Service `json:"service"`
20-
Location string `json:"location"`
18+
Project string `json:"project"`
19+
Service run.Service `json:"service"`
20+
Location string `json:"location"`
21+
IamPolicy run.IamPolicy `json:"iamPolicy,omitempty"`
2122
}
2223

2324
// ServiceStatus defines the observed state of Service

pkg/apis/cloudruncontroller/v1alpha1/zz_generated.deepcopy.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/apis/cloudruncontroller/v1alpha1/zz_generated.openapi.go

Lines changed: 21 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/controller/service/service_controller.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,18 @@ func (r *ReconcileService) Reconcile(request reconcile.Request) (reconcile.Resul
100100
return reconcile.Result{}, err
101101
}
102102

103+
// Be sure namespace is correctly set
104+
if instance.Spec.Service.Metadata.Namespace == "" {
105+
instance.Spec.Service.Metadata.Namespace = instance.Spec.Project
106+
}
107+
103108
rm, err := run.NewRunManager(instance.Spec.Project)
104109
if err != nil {
105110
return reconcile.Result{}, err
106111
}
107112

108113
parent := utils.Parent(instance.Spec.Project, instance.Spec.Location)
114+
resource := utils.ServiceName(parent, instance.Spec.Service.Metadata.Name)
109115

110116
if r.finalizer.IsDeletionCandidate(instance) {
111117
if value, exists := instance.GetAnnotations()[annotationDeletion]; exists && value == "true" {
@@ -117,12 +123,11 @@ func (r *ReconcileService) Reconcile(request reconcile.Request) (reconcile.Resul
117123
r.finalizer.Remove(instance)
118124
return reconcile.Result{}, r.client.Update(context.TODO(), instance)
119125
}
126+
r.finalizer.Add(instance)
120127

121-
// Be sure namespace is correctly set
122-
if instance.Spec.Service.Metadata.Namespace == "" {
123-
instance.Spec.Service.Metadata.Namespace = instance.Spec.Project
128+
if err := rm.SetIamPolicy(resource, instance.Spec.IamPolicy); err != nil {
129+
return reconcile.Result{}, err
124130
}
125-
r.finalizer.Add(instance)
126131

127132
if err := rm.CreateOrUpdate(parent, instance.Spec.Service); err != nil {
128133
return reconcile.Result{}, err

pkg/run/run.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,9 @@ func (rm *RunManager) Delete(parent string, service Service) error {
103103
_, err := rm.service.Projects.Locations.Services.Delete(name).Do()
104104
return err
105105
}
106+
107+
func (rm *RunManager) SetIamPolicy(resource string, policy IamPolicy) error {
108+
p := runApi.Policy(policy)
109+
_, err := rm.service.Projects.Locations.Services.SetIamPolicy(resource, &runApi.SetIamPolicyRequest{Policy: &p}).Do()
110+
return err
111+
}

pkg/run/service.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ import (
44
runApi "google.golang.org/api/run/v1alpha1"
55
)
66

7-
// type Service runApi.Service
8-
9-
// type Service struct {
10-
// *runApi.Service
11-
// }
12-
137
type Service runApi.Service
148

159
func (in *Service) DeepCopy() *Service {
@@ -25,3 +19,19 @@ func (in *Service) DeepCopyInto(out *Service) {
2519
*out = *in
2620
return
2721
}
22+
23+
type IamPolicy runApi.Policy
24+
25+
func (in *IamPolicy) DeepCopy() *IamPolicy {
26+
if in == nil {
27+
return nil
28+
}
29+
out := new(IamPolicy)
30+
in.DeepCopyInto(out)
31+
return out
32+
}
33+
34+
func (in *IamPolicy) DeepCopyInto(out *IamPolicy) {
35+
*out = *in
36+
return
37+
}

0 commit comments

Comments
 (0)