From 4a601d77f03699b07d335cc56cd749c17117e720 Mon Sep 17 00:00:00 2001 From: jiangxin Date: Tue, 28 Jun 2022 11:27:22 +0800 Subject: [PATCH] =?UTF-8?q?BIE-V2=E6=94=AF=E6=8C=81=E7=BA=AF=E7=B2=B9?= =?UTF-8?q?=E7=9A=84YAML=20(#469)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * BIE-V2支持纯粹的YAML * BIE-V2支持纯粹的YAML --- api/yaml.go | 43 +++++++++++++++---- api/yaml_app_test.go | 98 +++++++++++++++++++++++++++++++++++--------- api/yaml_test.go | 3 +- 3 files changed, 116 insertions(+), 28 deletions(-) diff --git a/api/yaml.go b/api/yaml.go index 415ccc2b..a7e1e0c8 100644 --- a/api/yaml.go +++ b/api/yaml.go @@ -228,7 +228,11 @@ func (api *API) generateSecret(ns string, r runtime.Object) (interface{}, error) return nil, common.Error(common.ErrRequestParamInvalid, common.Field("error", "secret name is already in use")) } - return api.Facade.CreateSecret(ns, secret) + res, err := api.Facade.CreateSecret(ns, secret) + if err != nil { + return nil, err + } + return api.ToFilteredSecretView(res), nil } func (api *API) updateSecret(ns string, r runtime.Object) (interface{}, error) { @@ -256,7 +260,11 @@ func (api *API) updateSecret(ns string, r runtime.Object) (interface{}, error) { secret.Version = oldSecret.Version secret.UpdateTimestamp = time.Now() - return api.Facade.UpdateSecret(ns, secret) + res, err := api.Facade.UpdateSecret(ns, secret) + if err != nil { + return nil, err + } + return api.ToSecretView(res), nil } func (api *API) deleteSecret(ns string, r runtime.Object) (string, error) { @@ -382,6 +390,9 @@ func (api *API) generateCommonSecret(ns string, s *corev1.Secret) (*specV1.Secre return nil, err } secret.Namespace = ns + secret.Labels = common.AddSystemLabel(secret.Labels, map[string]string{ + specV1.SecretLabel: specV1.SecretConfig, + }) return secret, nil } @@ -441,7 +452,7 @@ func (api *API) generateConfig(ns, userId string, r runtime.Object) (interface{} return nil, err } - return config, nil + return api.ToConfigurationView(config) } func (api *API) updateConfig(ns, userId string, r runtime.Object) (interface{}, error) { @@ -485,7 +496,12 @@ func (api *API) updateConfig(ns, userId string, r runtime.Object) (interface{}, config.UpdateTimestamp = time.Now() config.CreationTimestamp = res.CreationTimestamp - return api.Facade.UpdateConfig(ns, config) + res, err = api.Facade.UpdateConfig(ns, config) + if err != nil { + return nil, err + } + + return api.ToConfigurationView(res) } func (api *API) deleteConfig(ns string, r runtime.Object) (string, error) { @@ -520,7 +536,6 @@ func (api *API) deleteConfig(ns string, r runtime.Object) (string, error) { common.Field("name", cfg.Name)) } - //TODO: should remove file(bos/aws) of a function Config return cfg.Name, api.Facade.DeleteConfig(ns, cfg.Name) } @@ -640,7 +655,7 @@ func (api *API) generateApplication(ns string, r runtime.Object) (interface{}, e return nil, err } - return app, nil + return api.ToApplicationView(app) } func (api *API) updateApplication(ns string, r runtime.Object) (interface{}, error) { @@ -674,7 +689,12 @@ func (api *API) updateApplication(ns string, r runtime.Object) (interface{}, err app.Version = oldApp.Version app.CreationTimestamp = oldApp.CreationTimestamp - return api.Facade.UpdateApp(ns, oldApp, app, nil) + app, err = api.Facade.UpdateApp(ns, oldApp, app, nil) + if err != nil { + return nil, errors.Trace(err) + } + + return api.ToApplicationView(app) } func (api *API) deleteApplication(ns string, r runtime.Object) (string, error) { @@ -904,6 +924,15 @@ func (api *API) generateCommonAppInfo(ns string, podSpec *corev1.PodSpec) (*spec if !isRegistrySecret(*registry) { return nil, common.Error(common.ErrRequestParamInvalid, common.Field("error", "imagePullSecrets is not registry type")) } + secretVolume := specV1.Volume{ + Name: v.Name, + VolumeSource: specV1.VolumeSource{ + Secret: &specV1.ObjectReference{ + Name: v.Name, + }, + }, + } + volumes = append(volumes, secretVolume) } var svcs []specV1.Service diff --git a/api/yaml_app_test.go b/api/yaml_app_test.go index 5e9b521b..85a0c361 100644 --- a/api/yaml_app_test.go +++ b/api/yaml_app_test.go @@ -394,6 +394,14 @@ func TestAPI_CreateDeployApp(t *testing.T) { }, }, }, + { + Name: "myregistrykey", + VolumeSource: specV1.VolumeSource{ + Secret: &specV1.ObjectReference{ + Name: "myregistrykey", + }, + }, + }, }, Replica: 1, Workload: "deployment", @@ -412,8 +420,8 @@ func TestAPI_CreateDeployApp(t *testing.T) { } sApp.EXPECT().Get("default", "nginx", "").Return(nil, nil).Times(1) sConfig.EXPECT().Get("default", "common-cm", "").Return(cfg, nil).Times(2) - sSecret.EXPECT().Get("default", "dcell", "").Return(secret, nil).Times(2) - sSecret.EXPECT().Get("default", "myregistrykey", "").Return(registry, nil).Times(2) + sSecret.EXPECT().Get("default", "dcell", "").Return(secret, nil).Times(4) + sSecret.EXPECT().Get("default", "myregistrykey", "").Return(registry, nil).Times(4) sFacade.EXPECT().CreateApp("default", nil, gomock.Any(), nil).Return(deployApp, nil).Times(1) buf := new(bytes.Buffer) @@ -429,7 +437,7 @@ func TestAPI_CreateDeployApp(t *testing.T) { router.ServeHTTP(re, req) assert.Equal(t, http.StatusOK, re.Code) - var resApp []specV1.Application + var resApp []gmodels.ApplicationView body, err := ioutil.ReadAll(re.Body) assert.NilError(t, err) err = json.Unmarshal(body, &resApp) @@ -440,7 +448,8 @@ func TestAPI_CreateDeployApp(t *testing.T) { resapp, err := api.generateDeployApp("default", deploy) resapp.Services[0].Resources.Limits = nil resapp.Services[0].Resources.Requests = nil - assert.DeepEqual(t, &resApp[0], resapp) + appView, _ := api.ToApplicationView(resapp) + assert.DeepEqual(t, &resApp[0], appView) } func TestAPI_CreateDaemonSetApp(t *testing.T) { @@ -538,6 +547,14 @@ func TestAPI_CreateDaemonSetApp(t *testing.T) { }, }, }, + { + Name: "myregistrykey", + VolumeSource: specV1.VolumeSource{ + Secret: &specV1.ObjectReference{ + Name: "myregistrykey", + }, + }, + }, }, Workload: "daemonset", } @@ -555,8 +572,8 @@ func TestAPI_CreateDaemonSetApp(t *testing.T) { } sApp.EXPECT().Get("default", "nginx", "").Return(nil, nil).Times(1) sConfig.EXPECT().Get("default", "common-cm", "").Return(cfg, nil).Times(2) - sSecret.EXPECT().Get("default", "dcell", "").Return(secret, nil).Times(2) - sSecret.EXPECT().Get("default", "myregistrykey", "").Return(registry, nil).Times(2) + sSecret.EXPECT().Get("default", "dcell", "").Return(secret, nil).Times(4) + sSecret.EXPECT().Get("default", "myregistrykey", "").Return(registry, nil).Times(4) sFacade.EXPECT().CreateApp("default", nil, gomock.Any(), nil).Return(dsApp, nil).Times(1) buf := new(bytes.Buffer) @@ -572,7 +589,7 @@ func TestAPI_CreateDaemonSetApp(t *testing.T) { router.ServeHTTP(re, req) assert.Equal(t, http.StatusOK, re.Code) - var resApp []specV1.Application + var resApp []gmodels.ApplicationView body, err := ioutil.ReadAll(re.Body) assert.NilError(t, err) err = json.Unmarshal([]byte(body), &resApp) @@ -581,7 +598,8 @@ func TestAPI_CreateDaemonSetApp(t *testing.T) { resources := api.parseK8SYaml([]byte(testAppDs)) ds, _ := resources[0].(*appv1.DaemonSet) resapp, err := api.generateDaemonSetApp("default", ds) - assert.DeepEqual(t, &resApp[0], resapp) + appView, _ := api.ToApplicationView(resapp) + assert.DeepEqual(t, &resApp[0], appView) } func TestAPI_CreateJobApp(t *testing.T) { @@ -642,7 +660,7 @@ func TestAPI_CreateJobApp(t *testing.T) { router.ServeHTTP(re, req) assert.Equal(t, http.StatusOK, re.Code) - var resApp []specV1.Application + var resApp []gmodels.ApplicationView body, err := ioutil.ReadAll(re.Body) assert.NilError(t, err) err = json.Unmarshal([]byte(body), &resApp) @@ -652,7 +670,10 @@ func TestAPI_CreateJobApp(t *testing.T) { job, _ := resources[0].(*batchv1.Job) resapp, err := api.generateJobApp("default", job) resapp.Services[0].Resources = nil - assert.DeepEqual(t, &resApp[0], resapp) + appView, _ := api.ToApplicationView(resapp) + appView.Volumes = nil + appView.Registries = nil + assert.DeepEqual(t, &resApp[0], appView) } func TestAPI_UpdateDeployApp(t *testing.T) { @@ -747,6 +768,14 @@ func TestAPI_UpdateDeployApp(t *testing.T) { }, }, }, + { + Name: "myregistrykey", + VolumeSource: specV1.VolumeSource{ + Secret: &specV1.ObjectReference{ + Name: "myregistrykey", + }, + }, + }, }, Replica: 1, Workload: "deployment", @@ -826,6 +855,14 @@ func TestAPI_UpdateDeployApp(t *testing.T) { }, }, }, + { + Name: "myregistrykey", + VolumeSource: specV1.VolumeSource{ + Secret: &specV1.ObjectReference{ + Name: "myregistrykey", + }, + }, + }, }, Replica: 1, Workload: "deployment", @@ -845,8 +882,8 @@ func TestAPI_UpdateDeployApp(t *testing.T) { sApp.EXPECT().Get("default", "nginx", "").Return(expectApp, nil).Times(1) sConfig.EXPECT().Get("default", "common-cm", "").Return(cfg, nil).Times(2) - sSecret.EXPECT().Get("default", "dcell", "").Return(secret, nil).Times(2) - sSecret.EXPECT().Get("default", "myregistrykey", "").Return(registry, nil).Times(2) + sSecret.EXPECT().Get("default", "dcell", "").Return(secret, nil).Times(4) + sSecret.EXPECT().Get("default", "myregistrykey", "").Return(registry, nil).Times(4) sFacade.EXPECT().UpdateApp("default", expectApp, gomock.Any(), nil).Return(updateApp, nil).Times(1) buf := new(bytes.Buffer) @@ -862,7 +899,7 @@ func TestAPI_UpdateDeployApp(t *testing.T) { router.ServeHTTP(re, req) assert.Equal(t, http.StatusOK, re.Code) - var resApp []specV1.Application + var resApp []gmodels.ApplicationView body, err := ioutil.ReadAll(re.Body) assert.NilError(t, err) err = json.Unmarshal(body, &resApp) @@ -873,7 +910,8 @@ func TestAPI_UpdateDeployApp(t *testing.T) { resapp, err := api.generateDeployApp("default", deploy) resapp.Services[0].Resources.Limits = nil resapp.Services[0].Resources.Requests = nil - assert.DeepEqual(t, &resApp[0], resapp) + appView, _ := api.ToApplicationView(resapp) + assert.DeepEqual(t, &resApp[0], appView) } func TestAPI_UpdateDsApp(t *testing.T) { @@ -971,6 +1009,14 @@ func TestAPI_UpdateDsApp(t *testing.T) { }, }, }, + { + Name: "myregistrykey", + VolumeSource: specV1.VolumeSource{ + Secret: &specV1.ObjectReference{ + Name: "myregistrykey", + }, + }, + }, }, Workload: "daemonset", } @@ -1052,6 +1098,14 @@ func TestAPI_UpdateDsApp(t *testing.T) { }, }, }, + { + Name: "myregistrykey", + VolumeSource: specV1.VolumeSource{ + Secret: &specV1.ObjectReference{ + Name: "myregistrykey", + }, + }, + }, }, Workload: "daemonset", } @@ -1070,8 +1124,8 @@ func TestAPI_UpdateDsApp(t *testing.T) { sApp.EXPECT().Get("default", "nginx", "").Return(dsApp, nil).Times(1) sConfig.EXPECT().Get("default", "common-cm", "").Return(cfg, nil).Times(2) - sSecret.EXPECT().Get("default", "dcell", "").Return(secret, nil).Times(2) - sSecret.EXPECT().Get("default", "myregistrykey", "").Return(registry, nil).Times(2) + sSecret.EXPECT().Get("default", "dcell", "").Return(secret, nil).Times(4) + sSecret.EXPECT().Get("default", "myregistrykey", "").Return(registry, nil).Times(4) sFacade.EXPECT().UpdateApp("default", dsApp, gomock.Any(), nil).Return(updateApp, nil).Times(1) buf := new(bytes.Buffer) @@ -1087,7 +1141,7 @@ func TestAPI_UpdateDsApp(t *testing.T) { router.ServeHTTP(re, req) assert.Equal(t, http.StatusOK, re.Code) - var resApp []specV1.Application + var resApp []gmodels.ApplicationView body, err := ioutil.ReadAll(re.Body) assert.NilError(t, err) err = json.Unmarshal(body, &resApp) @@ -1096,7 +1150,8 @@ func TestAPI_UpdateDsApp(t *testing.T) { resources := api.parseK8SYaml([]byte(updateAppDs)) ds, _ := resources[0].(*appv1.DaemonSet) resapp, err := api.generateDaemonSetApp("default", ds) - assert.DeepEqual(t, &resApp[0], resapp) + appView, _ := api.ToApplicationView(resapp) + assert.DeepEqual(t, &resApp[0], appView) } func TestAPI_UpdateJobApp(t *testing.T) { @@ -1181,7 +1236,7 @@ func TestAPI_UpdateJobApp(t *testing.T) { router.ServeHTTP(re, req) assert.Equal(t, http.StatusOK, re.Code) - var resApp []specV1.Application + var resApp []gmodels.ApplicationView body, err := ioutil.ReadAll(re.Body) assert.NilError(t, err) err = json.Unmarshal(body, &resApp) @@ -1191,7 +1246,10 @@ func TestAPI_UpdateJobApp(t *testing.T) { job, _ := resources[0].(*batchv1.Job) resapp, err := api.generateJobApp("default", job) resapp.Services[0].Resources = nil - assert.DeepEqual(t, &resApp[0], resapp) + appView, _ := api.ToApplicationView(resapp) + appView.Registries = nil + appView.Volumes = nil + assert.DeepEqual(t, &resApp[0], appView) } func TestAPI_DeleteApp(t *testing.T) { diff --git a/api/yaml_test.go b/api/yaml_test.go index 1d8a264b..d256f32a 100644 --- a/api/yaml_test.go +++ b/api/yaml_test.go @@ -278,7 +278,8 @@ func TestAPI_CreateSecret(t *testing.T) { Name: "dcell", Namespace: "default", Labels: map[string]string{ - "secret": "dcell", + "secret": "dcell", + specV1.SecretLabel: specV1.SecretConfig, }, Annotations: map[string]string{ "secret": "dcell",