Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Support specifying publishVersion when creating or updating oam applications #888

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
4 changes: 2 additions & 2 deletions .github/workflows/server-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ jobs:
with:
version: 3.1.0
kubebuilderOnly: false
kubernetesVersion: v1.21.2
kubernetesVersion: v1.22.17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 1.21 is the oldest version we need to support, right? cc @chivalryq

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we'd better revert the change back to 1.21

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we should find some way to make the k3s setup works again, currently the k3s setup script only support latest 200 versions ( which >= v1.24.7-rc3+k3s1, and continue growth day by day):

image

curl --silent --fail --location "${authz[@]-}"  "https://api.github.com/repos/k3s-io/k3s/releases?per_page=999&page=2"

image

maybe we should find another k3s setup script to workaround this issue.


- name: Run api server unit test
run: make unit-test-server
Expand All @@ -110,7 +110,7 @@ jobs:
if: needs.detect-noop.outputs.noop != 'true'
strategy:
matrix:
k8s-version: ["v1.21", "v1.26"]
k8s-version: ["v1.22", "v1.26"]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ matrix.k8s-version }}
cancel-in-progress: true
Expand Down
2 changes: 1 addition & 1 deletion addon/metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: velaux
version: v1.8.0-rc.3
version: v1.8.0-rc.4
description: KubeVela User Experience (UX). An extensible, application-oriented delivery and management Platform.
icon: https://static.kubevela.net/images/logos/KubeVela%20-03.png
url: https://kubevela.io
Expand Down
32 changes: 32 additions & 0 deletions e2e-test/oam_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,36 @@ var _ = Describe("Test oam application rest api", func() {
defer res.Body.Close()
}, time.Minute).Should(Succeed())
})

It("Test dryRun create and update oam app with publishVersion parameter", func() {
defer GinkgoRecover()
By("test dryRun create app with publishVersion")

Expect(common.ReadYamlToObject("./testdata/dryrun-app.yaml", &app)).Should(BeNil())
req := apiv1.ApplicationRequest{
Components: app.Spec.Components,
Policies: app.Spec.Policies,
Workflow: app.Spec.Workflow,
}
publishVersion := time.Now().Format(appName + "20060102150405")
res := post(fmt.Sprintf("/v1/namespaces/%s/applications/%s?dryRun=All&publishVersion=%s", namespace, appName+"-dryrun", publishVersion), req)
Expect(res).ShouldNot(BeNil())
Expect(cmp.Diff(res.StatusCode, 200)).Should(BeEmpty())
Expect(res.Body).ShouldNot(BeNil())
defer res.Body.Close()

time.Sleep(time.Second)
By("test dryRun update app with publishVersion")
updateReq := apiv1.ApplicationRequest{
Components: app.Spec.Components[1:],
}
publishVersion = time.Now().Format(appName + "20060102150405")
Eventually(func(g Gomega) {
res = post(fmt.Sprintf("/v1/namespaces/%s/applications/%s?dryRun=All&publishVersion=%s", namespace, appName+"-dryrun", publishVersion), updateReq)
g.Expect(res).ShouldNot(BeNil())
g.Expect(cmp.Diff(res.StatusCode, 200)).Should(BeEmpty())
g.Expect(res.Body).ShouldNot(BeNil())
defer res.Body.Close()
}, time.Minute).Should(Succeed())
})
})
30 changes: 22 additions & 8 deletions pkg/server/domain/service/oam_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@

// OAMApplicationService oam_application service
type OAMApplicationService interface {
CreateOrUpdateOAMApplication(context.Context, apisv1.ApplicationRequest, string, string) error
CreateOrUpdateOAMApplication(context.Context, apisv1.ApplicationRequest, string, string, string) error
GetOAMApplication(context.Context, string, string) (*apisv1.ApplicationResponse, error)
DeleteOAMApplication(context.Context, string, string) error
DryRunOAMApplication(context.Context, apisv1.ApplicationRequest, string, string) error
DryRunOAMApplication(context.Context, apisv1.ApplicationRequest, string, string, string) error
}

// NewOAMApplicationService new oam_application service
Expand All @@ -51,7 +51,7 @@
}

// CreateOrUpdateOAMApplication create or update application
func (o oamApplicationServiceImpl) CreateOrUpdateOAMApplication(ctx context.Context, request apisv1.ApplicationRequest, name, namespace string) error {
func (o oamApplicationServiceImpl) CreateOrUpdateOAMApplication(ctx context.Context, request apisv1.ApplicationRequest, name, namespace string, publishVersion string) error {
ns := new(v1.Namespace)
err := o.KubeClient.Get(ctx, client.ObjectKey{Name: namespace}, ns)
if kerrors.IsNotFound(err) {
Expand All @@ -61,10 +61,16 @@
}
}

annotations := map[string]string{}
if publishVersion != "" {
annotations["app.oam.dev/publishVersion"] = publishVersion
}

Check warning on line 67 in pkg/server/domain/service/oam_application.go

View check run for this annotation

Codecov / codecov/patch

pkg/server/domain/service/oam_application.go#L66-L67

Added lines #L66 - L67 were not covered by tests

app := &v1beta1.Application{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Name: name,
Namespace: namespace,
Annotations: annotations,
},
Spec: v1beta1.ApplicationSpec{
Components: request.Components,
Expand All @@ -83,6 +89,7 @@
}

existApp.Spec = app.Spec
existApp.ObjectMeta.Annotations = annotations
return o.KubeClient.Update(ctx, existApp)
}

Expand Down Expand Up @@ -111,7 +118,7 @@
}

// DryRunOAMApplication dryRun create or update application
func (o oamApplicationServiceImpl) DryRunOAMApplication(ctx context.Context, request apisv1.ApplicationRequest, name, namespace string) error {
func (o oamApplicationServiceImpl) DryRunOAMApplication(ctx context.Context, request apisv1.ApplicationRequest, name, namespace string, publishVersion string) error {
ns := new(v1.Namespace)
err := o.KubeClient.Get(ctx, client.ObjectKey{Name: namespace}, ns)
if kerrors.IsNotFound(err) {
Expand All @@ -121,14 +128,20 @@
}
}

annotations := map[string]string{}
if publishVersion != "" {
annotations["app.oam.dev/publishVersion"] = publishVersion
}

app := &v1beta1.Application{
TypeMeta: metav1.TypeMeta{
Kind: "Application",
APIVersion: "core.oam.dev/v1beta1",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Name: name,
Namespace: namespace,
Annotations: annotations,
},
Spec: v1beta1.ApplicationSpec{
Components: request.Components,
Expand Down Expand Up @@ -157,5 +170,6 @@
}

existApp.Spec = app.Spec
existApp.ObjectMeta.Annotations = annotations

Check warning on line 173 in pkg/server/domain/service/oam_application.go

View check run for this annotation

Codecov / codecov/patch

pkg/server/domain/service/oam_application.go#L173

Added line #L173 was not covered by tests
return o.KubeClient.Update(ctx, existApp, client.DryRunAll)
}
35 changes: 31 additions & 4 deletions pkg/server/domain/service/oam_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ var _ = Describe("Test oam application service function", func() {
Policies: baseApp.Spec.Policies,
Workflow: baseApp.Spec.Workflow,
}
Expect(oamAppService.CreateOrUpdateOAMApplication(ctx, req, appName, appNs)).Should(BeNil())
Expect(oamAppService.CreateOrUpdateOAMApplication(ctx, req, appName, appNs, "")).Should(BeNil())

app := new(v1beta1.Application)
Expect(k8sClient.Get(ctx, client.ObjectKey{Namespace: appNs, Name: appName}, app)).Should(BeNil())
Expand All @@ -92,7 +92,7 @@ var _ = Describe("Test oam application service function", func() {
updateReq := apiv1.ApplicationRequest{
Components: baseApp.Spec.Components[1:],
}
Expect(oamAppService.CreateOrUpdateOAMApplication(ctx, updateReq, appName, appNs)).Should(BeNil())
Expect(oamAppService.CreateOrUpdateOAMApplication(ctx, updateReq, appName, appNs, "")).Should(BeNil())

updatedApp := new(v1beta1.Application)
Expect(k8sClient.Get(ctx, client.ObjectKey{Namespace: appNs, Name: appName}, updatedApp)).Should(BeNil())
Expand Down Expand Up @@ -137,13 +137,40 @@ var _ = Describe("Test oam application service function", func() {
Policies: newApp.Spec.Policies,
Workflow: newApp.Spec.Workflow,
}
Expect(oamAppService.DryRunOAMApplication(ctx, req, appName, appNs)).Should(BeNil())
Expect(oamAppService.DryRunOAMApplication(ctx, req, appName, appNs, "")).Should(BeNil())

By("test dryRun update application")
updateReq := apiv1.ApplicationRequest{
Components: newApp.Spec.Components[1:],
}
Expect(oamAppService.DryRunOAMApplication(ctx, updateReq, appName, appNs)).Should(BeNil())
Expect(oamAppService.DryRunOAMApplication(ctx, updateReq, appName, appNs, "")).Should(BeNil())

})

It("Test DryRunOAMApplication function with publishVersion", func() {
By("test dryRun create application")
appName := "test-new-app"
appNs := randomNamespaceName("test-new-app")
publishVersion := time.Now().Format(appName + "20060102150405")
var newApp v1beta1.Application
Expect(common.ReadYamlToObject("./testdata/dryrun-app.yaml", &newApp)).Should(BeNil())
newApp.SetNamespace(namespace)
Eventually(func() error {
return k8sClient.Create(ctx, &newApp)
}, time.Second*3, time.Microsecond*300).Should(SatisfyAny(BeNil(), &util.AlreadyExistMatcher{}))

req := apiv1.ApplicationRequest{
Components: newApp.Spec.Components,
Policies: newApp.Spec.Policies,
Workflow: newApp.Spec.Workflow,
}
Expect(oamAppService.DryRunOAMApplication(ctx, req, appName, appNs, publishVersion)).Should(BeNil())

By("test dryRun update application")
updateReq := apiv1.ApplicationRequest{
Components: newApp.Spec.Components[1:],
}
Expect(oamAppService.DryRunOAMApplication(ctx, updateReq, appName, appNs, publishVersion)).Should(BeNil())

})
})
11 changes: 8 additions & 3 deletions pkg/server/interfaces/api/oam_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,15 @@ func (c *oamApplication) GetWebServiceRoute() *restful.WebService {
Param(ws.QueryParameter("dryRun", "When present, indicates that modifications should not be persisted. "+
"An invalid or unrecognized dryRun directive will result in an error response and no further processing of the request. "+
"Valid values are: - All: all dry run stages will be processed").DataType("string").Required(false)).
Param(ws.QueryParameter("publishVersion", "The publish version for deploying application."+
"if no specified, {application name}-v{the number of times it was published} (e.g.: demo-app-v13) will be used. "+
"the workflow record will use this value as the record identifier, so please ensure that this value is not duplicated "+
"if you specify it, otherwise the request will report an error").DataType("string").Required(false)).
Reads(apis.ApplicationRequest{}))

ws.Route(ws.DELETE("/namespaces/{namespace}/applications/{appname}").To(c.deleteApplication).
Operation("deleteOAMApplication").
Doc("create or update oam application in the specified namespace").
Doc("delete oam application in the specified namespace").
Metadata(restfulspec.KeyOpenAPITags, tags).
Filter(c.RbacService.CheckPerm("application", "delete")).
Param(ws.PathParameter("namespace", "identifier of the namespace").DataType("string")).
Expand Down Expand Up @@ -104,19 +108,20 @@ func (c *oamApplication) createOrUpdateApplication(req *restful.Request, res *re
return
}

publishVersion := req.QueryParameter("publishVersion")
dryRun := req.QueryParameter("dryRun")
if len(dryRun) != 0 {
if dryRun != "All" {
bcode.ReturnError(req, res, bcode.ErrApplicationDryRunFailed.SetMessage("Invalid dryRun parameter. Must be 'All'"))
return
}
if err := c.OamApplicationService.DryRunOAMApplication(req.Request.Context(), createReq, appName, namespace); err != nil {
if err := c.OamApplicationService.DryRunOAMApplication(req.Request.Context(), createReq, appName, namespace, publishVersion); err != nil {
klog.Errorf("dryrun application failure %s", err.Error())
bcode.ReturnError(req, res, err)
return
}
} else {
if err := c.OamApplicationService.CreateOrUpdateOAMApplication(req.Request.Context(), createReq, appName, namespace); err != nil {
if err := c.OamApplicationService.CreateOrUpdateOAMApplication(req.Request.Context(), createReq, appName, namespace, publishVersion); err != nil {
klog.Errorf("create application failure %s", err.Error())
bcode.ReturnError(req, res, err)
return
Expand Down
Loading