Skip to content

Commit

Permalink
BIE-V2支持纯粹的YAML (#469)
Browse files Browse the repository at this point in the history
* BIE-V2支持纯粹的YAML

* BIE-V2支持纯粹的YAML
  • Loading branch information
Meteriox committed Jun 28, 2022
1 parent 2c43fa0 commit 4a601d7
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 28 deletions.
43 changes: 36 additions & 7 deletions api/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
98 changes: 78 additions & 20 deletions api/yaml_app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,14 @@ func TestAPI_CreateDeployApp(t *testing.T) {
},
},
},
{
Name: "myregistrykey",
VolumeSource: specV1.VolumeSource{
Secret: &specV1.ObjectReference{
Name: "myregistrykey",
},
},
},
},
Replica: 1,
Workload: "deployment",
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -538,6 +547,14 @@ func TestAPI_CreateDaemonSetApp(t *testing.T) {
},
},
},
{
Name: "myregistrykey",
VolumeSource: specV1.VolumeSource{
Secret: &specV1.ObjectReference{
Name: "myregistrykey",
},
},
},
},
Workload: "daemonset",
}
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -747,6 +768,14 @@ func TestAPI_UpdateDeployApp(t *testing.T) {
},
},
},
{
Name: "myregistrykey",
VolumeSource: specV1.VolumeSource{
Secret: &specV1.ObjectReference{
Name: "myregistrykey",
},
},
},
},
Replica: 1,
Workload: "deployment",
Expand Down Expand Up @@ -826,6 +855,14 @@ func TestAPI_UpdateDeployApp(t *testing.T) {
},
},
},
{
Name: "myregistrykey",
VolumeSource: specV1.VolumeSource{
Secret: &specV1.ObjectReference{
Name: "myregistrykey",
},
},
},
},
Replica: 1,
Workload: "deployment",
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -971,6 +1009,14 @@ func TestAPI_UpdateDsApp(t *testing.T) {
},
},
},
{
Name: "myregistrykey",
VolumeSource: specV1.VolumeSource{
Secret: &specV1.ObjectReference{
Name: "myregistrykey",
},
},
},
},
Workload: "daemonset",
}
Expand Down Expand Up @@ -1052,6 +1098,14 @@ func TestAPI_UpdateDsApp(t *testing.T) {
},
},
},
{
Name: "myregistrykey",
VolumeSource: specV1.VolumeSource{
Secret: &specV1.ObjectReference{
Name: "myregistrykey",
},
},
},
},
Workload: "daemonset",
}
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion api/yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 4a601d7

Please sign in to comment.