Skip to content

Commit cb20df6

Browse files
nivr4Niv RavivshaiMoria
authored
feat: add bucket custom resource status field
* add kubebuild init files, titfile and few command to makefile for running locally * add kind config yaml for local k8s kind cluster * modify the make file , deploy working * define s3 bucket type * add readme and script for local running * add input validition and RetryPeriod * edit titfile * add aws client functions * insert create and delete bucket functionality , modify the tiltfile * add gettter to aws client , and helm commands to localstack * support encrypt bucket * add resource map to manage k8s resource agianst aws resource * fix delete resource * fix run loclal script, add logs to delete flow * fix deletion of bucket in aws * support deletion of bucket policy and creation and deletion of iam role * support deletion of bucket policy and creation and deletion of iam role * add bucket name validation * bucketname validation * syntax * refactor: remove unused files in bin folder + update git ignore * fix: change runLocalenv.sh script variable defenitions * add tests folder * update git ignore * add varieble file to config * remove region from s3 resource * move cred var to deploy yaml * fix: makefile controller-gen * add devmode var * add to readme: golang version * add cleanup bucket function to delete flow * move config varibels under controllers folder * edit logs * test * change delete logic * edit logs * chang looger logic, fix put tgs function * add logs for update flow * remove comment * a * change kind cluster * add system test , edit run local script to deploy ingress controller * change port to 4566, fix putting tags * edit: readme, add update test * crate k8s client function * crate k8s client function * fix scripts * edit delete bucket test * add k8s manger * add logs to tests, fix update tags function and edit logs * add CRUD service account function * add service account functionality, create small app to test conectivety to s3 * merge brunch * add yaml files for testing, add integration test, add test app * fix integration test * change busy wait function, add valisation to edit flow * fix system test, add to local env empty app, fix iam role function * edit readme , add script for upload test app * add service account functions * code refactor, add verible to config * change file and folder name * fix bug in generate rbac * refactor aws client * add mount volume for token * fix package-lock.json * add configmap for define body, add cluster role for app * refactor k8s client * add yaml file for deploying srvices * add tests for testing response from auth server * add example for github action * add create kind cluster to pipline * add create kind cluster to pipline * add create kind cluster to pipline * add create kind cluster to pipline * ci * fix bug in create service account flow , add tests * add to test pods check * change tests to eventually check * add ci * add ci * add ci * add ci * add ci * add ci * add ci * add ci * add ci * add ci * add ci * add ci * add ci * add ci * add ci * add ci * add ci * add ci * add ci * add ci * add greeting workflow * add ci for create local env and run tests on it * add branch name to ci * add branch name to ci * add unit test * change check metch app , add tests to other pod controllers * remove unuse code * add unit test * add status for bucket * change findPodsController function * rename file * change bucket status to string, add tests * fix system test * change unit tests * add unit test to readme * add unit test to readme * add retry for system tests * add sleep time between tests * add update status function to reflact bucket status * change update status function Co-authored-by: Niv Raviv <[email protected]> Co-authored-by: shaimoria <[email protected]>
1 parent 5d2c56f commit cb20df6

File tree

8 files changed

+62
-11
lines changed

8 files changed

+62
-11
lines changed

api/v1/s3bucket_types.go

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ type S3BucketSpec struct {
4040

4141
// S3BucketStatus defines the observed state of S3Bucket
4242
type S3BucketStatus struct {
43+
// +kubebuilder:default:=failed
44+
Status string `json:"status"`
4345
}
4446

4547
//+kubebuilder:object:root=true

config/crd/bases/s3operator.payu.com_s3buckets.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ spec:
5656
type: object
5757
status:
5858
description: S3BucketStatus defines the observed state of S3Bucket
59+
properties:
60+
status:
61+
default: failed
62+
type: string
63+
required:
64+
- status
5965
type: object
6066
type: object
6167
served: true

config/manager/manager.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ spec:
6868
value: test
6969
- name: AWS_ACCESS_KEY_ID
7070
value: test
71+
- name: DEVMODE
72+
value: "true"
7173
serviceAccountName: k8s-s3-operator-controller-manager
7274
volumes:
7375
- name: token

controllers/config/variables.go

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ var waitBackoffSteps int64
2525
var pathToToken string
2626
var SERVICE_ACCOUNT_APPROVAL_URL string
2727
var configMapName string
28+
const STATUS_FAIL = "failed"
29+
const STATUS_READY = "ready"
2830

2931
func init() {
3032
var err error

controllers/k8s/k8s-client.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -172,15 +172,15 @@ func (k *K8sClient) checkMatchingAppControllerToServiceAccount(SAName string, la
172172
}
173173

174174
err = errors.New("didnt find any match pod controller")
175-
k.Log.Error(err, "serviceaccount name", SAName, "labels", labelsFromS3)
175+
k.Log.Error(err, "didnt find any match pod controller", "serviceaccount_name", SAName, "labels", labelsFromS3)
176176
return "", err
177177
}
178178

179179
func (k *K8sClient) deleteServiceAccount(sa *v1.ServiceAccount) error {
180-
k.Log.Info("Delete service account", "serviceaccount name", sa.Name)
180+
k.Log.Info("Delete service account", "serviceaccount_name", sa.Name)
181181
err := k.Delete(context.Background(), sa)
182182
if err != nil {
183-
k.Log.Error(err, "error to delete service account", "serviceaccount name", sa.Name)
183+
k.Log.Error(err, "error to delete service account", "serviceaccount_name", sa.Name)
184184
}
185185
return err
186186
}

controllers/s3bucket_controller.go

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
s3operatorv1 "github.com/PayU/K8s-S3-Operator/api/v1"
2424
awsClient "github.com/PayU/K8s-S3-Operator/controllers/aws"
25+
"github.com/PayU/K8s-S3-Operator/controllers/config"
2526
k8s "github.com/PayU/K8s-S3-Operator/controllers/k8s"
2627

2728
"github.com/go-logr/logr"
@@ -79,6 +80,7 @@ func (r *S3BucketReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
7980
//succeded to get resource, check if need to create or update
8081
isbucketExists, err := r.AwsClient.IsBucketExists(s3Bucket.Name)
8182
if err != nil {
83+
r.updateBucketResourceStatus(&s3Bucket,config.STATUS_FAIL)
8284
return ctrl.Result{Requeue: true}, err
8385
}
8486
if isbucketExists {
@@ -87,8 +89,10 @@ func (r *S3BucketReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
8789
err = r.handleCreationFlow(&s3Bucket.Spec, s3Bucket.Name, req.Namespace)
8890
}
8991
if err != nil {
92+
r.updateBucketResourceStatus(&s3Bucket,config.STATUS_FAIL)
9093
return ctrl.Result{Requeue: true, RequeueAfter: time.Duration(10 * time.Second)}, err
9194
}
95+
r.updateBucketResourceStatus(&s3Bucket,config.STATUS_READY)
9296
return ctrl.Result{Requeue: false}, err
9397
}
9498

@@ -103,6 +107,7 @@ func (r *S3BucketReconciler) handleCreationFlow(bucketSpec *s3operatorv1.S3Bucke
103107
err := r.AwsClient.ValidateBucketName(bucketName)
104108
if err != nil {
105109
r.Log.Error(err, "bucket name is unvalid")
110+
106111
return err
107112
}
108113
// create or update service account
@@ -128,3 +133,10 @@ func (r *S3BucketReconciler) handleDeleteFlow(bucketSpec *s3operatorv1.S3BucketS
128133
isDelted, err := r.AwsClient.HandleBucketDeletion(bucketName)
129134
return isDelted, err
130135
}
136+
func (r *S3BucketReconciler)updateBucketResourceStatus( s3Bucket *s3operatorv1.S3Bucket, status string){
137+
s3Bucket.Status.Status = status
138+
errToUpdate := r.Client.Status().Update(context.Background(), s3Bucket)
139+
if errToUpdate != nil{
140+
r.Log.Error(errToUpdate,"didnt succeded to update status")
141+
}
142+
}

tests/integrationTests/integration_test.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ func TestRes500FromAuthServer(t *testing.T) {
172172
t.Log("TestRes500FromAuthServer")
173173
t.Cleanup(Cleanup)
174174
g := NewWithT(t)
175-
// setCounterToZero(t)
176175
//update auth server to err mode
177176
t.Log("update auth server to err mode")
178177
err := K8sApply("./yamlFiles/deployAuthServerErrMode.yaml")
@@ -204,15 +203,12 @@ func TestRes403FromAuthServer(t *testing.T) {
204203
err = K8sApply("./yamlFiles/testflow1.yaml")
205204
g.Expect(err).NotTo(HaveOccurred())
206205

207-
// time.Sleep(graceTimeAppChange * time.Second)
208-
209206
//check they created and running status
210207
validateResourceStatus(t, true, false, true, false, "deploy")
211208
// validateNumOfCallToAuthServer(t, 1)
212209

213210
}
214211
func validateResourceStatus(t *testing.T, expectPodController bool, expectSA bool, expectBucket bool, expectPods bool, podController string) {
215-
// g := NewWithT(t)
216212
switch podController {
217213
case "deploy":
218214
deploy := appsv1.Deployment{}
@@ -233,13 +229,22 @@ func validateResourceStatus(t *testing.T, expectPodController bool, expectSA boo
233229
case "demonset":
234230
demonset := appsv1.DaemonSet{}
235231
getResourceEventually(t, &demonset, expectPodController, appName)
236-
232+
237233
}
238234

239235
sa := v1.ServiceAccount{}
240236
getResourceEventually(t, &sa, expectSA, serviceAccountName)
241237
s3Bucket := s3operatorv1.S3Bucket{}
242238
getResourceEventually(t, &s3Bucket, expectBucket, s3BucketName)
239+
if expectSA && expectBucket {
240+
expectedStatus := "ready"
241+
checkBucketStatus(t, expectedStatus)
242+
}
243+
if expectBucket && !expectSA {
244+
expectedStatus := "failed"
245+
checkBucketStatus(t, expectedStatus)
246+
247+
}
243248

244249
}
245250

@@ -344,3 +349,13 @@ func checkPods(t *testing.T, expectPods bool) {
344349
}, 20*time.Second, 4*time.Second)
345350

346351
}
352+
func checkBucketStatus(t *testing.T, expectStaus string) {
353+
g := NewWithT(t)
354+
s3Bucket := s3operatorv1.S3Bucket{}
355+
g.Eventually(func() bool {
356+
err := k8sClient.Get(context.TODO(), types.NamespacedName{Namespace: namespace, Name: s3BucketName}, &s3Bucket)
357+
t.Log("expectStaus", expectStaus, "s3Bucket.Status.Status", s3Bucket.Status.Status)
358+
return err == nil && s3Bucket.Status.Status == expectStaus
359+
360+
}, 20*time.Second, 4*time.Second).Should(Equal(true))
361+
}

tests/systemTest/system_test.go

+15-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
s3operatorv1 "github.com/PayU/K8s-S3-Operator/api/v1"
1919
. "github.com/onsi/gomega"
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
"k8s.io/apimachinery/pkg/types"
2122

2223
"sigs.k8s.io/controller-runtime/pkg/client"
2324
"sigs.k8s.io/controller-runtime/pkg/log/zap"
@@ -29,7 +30,7 @@ var namespace = "k8s-s3-operator-system"
2930
var logger logr.Logger
3031
var k8sClient client.Client
3132
var s3Bucket s3operatorv1.S3Bucket
32-
var graceTime = time.Duration(5)
33+
var graceTime = time.Duration(7)
3334
var serviceAccountName = "system-test-serviceaccount"
3435
var appName = "system-test-app"
3536

@@ -50,7 +51,17 @@ func TestMain(m *testing.M) {
5051

5152
s3Bucket = s3operatorv1.S3Bucket{ObjectMeta: metav1.ObjectMeta{Name: bucketName, Namespace: namespace},
5253
Spec: s3operatorv1.S3BucketSpec{Serviceaccount: serviceAccountName, Selector: map[string]string{"app": appName}}}
53-
exitVal := m.Run()
54+
var exitVal int
55+
for i := 1; i <= 3; i++ { // retry to pass tests
56+
logger.Info("Run system tests", "tryNumber", i)
57+
exitVal = m.Run()
58+
if exitVal == 0 {
59+
logger.Info("pass all test", "tryNumber", i)
60+
break
61+
}else{
62+
time.Sleep(graceTime * time.Second)
63+
}
64+
}
5465
logger.Info("finish to run all tests")
5566

5667
os.Exit(exitVal)
@@ -87,8 +98,9 @@ func TestBucketUpdateTag(t *testing.T) {
8798
g.Expect(len(tags.TagSet)).Should(Equal(1))
8899

89100
t.Log("update bucket tags expect not to have error and to add the new tag")
101+
k8sClient.Get(context.Background(), types.NamespacedName{Namespace: namespace, Name: bucketName}, &s3Bucket)
90102
s3Bucket.Spec.Tags = map[string]string{"testKey": "testValue"}
91-
err = k8sClient.Update(context.Background(), &s3Bucket)
103+
err = k8sClient.Update(context.TODO(), &s3Bucket)
92104
g.Expect(err).NotTo(HaveOccurred())
93105
time.Sleep(graceTime * time.Second)
94106
tags, err = s3Client.GetBucketTagging(&s3.GetBucketTaggingInput{Bucket: aws.String(bucketName)})

0 commit comments

Comments
 (0)