Skip to content

Commit 37b8140

Browse files
committed
feat: op event publish;add opid
1 parent fbb710c commit 37b8140

32 files changed

+387
-165
lines changed

api/app.bytetrade.io/v1alpha1/appmanager_types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type ApplicationManagerStatus struct {
3131

3232
OpType OpType `json:"opType"`
3333
OpGeneration int64 `json:"opGeneration"`
34+
OpID string `json:"opId,omitempty"`
3435
State ApplicationManagerState `json:"state,omitempty"`
3536
OpRecords []OpRecord `json:"opRecords,omitempty"`
3637
Message string `json:"message,omitempty"`
@@ -56,6 +57,7 @@ type ApplicationManagerSpec struct {
5657
// OpRecord contains details of an operation.
5758
type OpRecord struct {
5859
OpType OpType `json:"opType"`
60+
OpID string `json:"opId,omitempty"`
5961
Message string `json:"message"`
6062
Version string `json:"version"`
6163
Source string `json:"source"`

config/crd/bases/app.bytetrade.io_applicationmanagers.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,16 @@ spec:
8989
opGeneration:
9090
format: int64
9191
type: integer
92+
opId:
93+
type: string
9294
opRecords:
9395
items:
9496
description: OpRecord contains details of an operation.
9597
properties:
9698
message:
9799
type: string
100+
opId:
101+
type: string
98102
opType:
99103
description: OpType represents the type of operation being performed.
100104
type: string

config/crd/bases/app.bytetrade.io_imagemanagers.yaml

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6-
controller-gen.kubebuilder.io/version: v0.9.2
7-
creationTimestamp: null
6+
controller-gen.kubebuilder.io/version: v0.14.0
87
name: imagemanagers.app.bytetrade.io
98
spec:
109
group: app.bytetrade.io
@@ -38,14 +37,19 @@ spec:
3837
description: ImageManager is the Schema for the image managers API
3938
properties:
4039
apiVersion:
41-
description: 'APIVersion defines the versioned schema of this representation
42-
of an object. Servers should convert recognized schemas to the latest
43-
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
40+
description: |-
41+
APIVersion defines the versioned schema of this representation of an object.
42+
Servers should convert recognized schemas to the latest internal value, and
43+
may reject unrecognized values.
44+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
4445
type: string
4546
kind:
46-
description: 'Kind is a string value representing the REST resource this
47-
object represents. Servers may infer this from the endpoint the client
48-
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
47+
description: |-
48+
Kind is a string value representing the REST resource this object represents.
49+
Servers may infer this from the endpoint the client submits requests to.
50+
Cannot be updated.
51+
In CamelCase.
52+
More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
4953
type: string
5054
metadata:
5155
type: object
@@ -91,9 +95,9 @@ spec:
9195
type: string
9296
type: object
9397
type: object
94-
description: 'INSERT ADDITIONAL STATUS FIELD - define observed state
95-
of cluster Important: Run "make" to regenerate code after modifying
96-
this file'
98+
description: |-
99+
INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
100+
Important: Run "make" to regenerate code after modifying this file
97101
type: object
98102
message:
99103
type: string

controllers/application_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ func (r *ApplicationReconciler) updateApplication(ctx context.Context, req ctrl.
401401
}
402402

403403
owner := deployment.GetLabels()[constants.ApplicationOwnerLabel]
404-
klog.Infof("in updateApplication ....")
404+
klog.Infof("in updateApplication ....appname: %v", app.Spec.Name)
405405
icons := getAppIcon(deployment)
406406
var icon string
407407

controllers/image_info_controller.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ func (r *AppImageInfoController) GetImageInfo(ctx context.Context, refs []string
278278

279279
manifest, err := r.GetManifest(ctx, replacedRef)
280280
if err != nil {
281-
return imageInfos, nil
281+
klog.Infof("get image %s manifest failed %v", name.String(), err)
282+
return imageInfos, err
282283
}
283284

284285
imageInfo := appv1alpha1.ImageInfo{
@@ -299,6 +300,7 @@ func (r *AppImageInfoController) GetImageInfo(ctx context.Context, refs []string
299300
_, err = r.imageClient.ContentStore().Info(ctx, layer.Digest)
300301
if err == nil {
301302
imageLayer.Offset = layer.Size
303+
imageLayers = append(imageLayers, imageLayer)
302304
// go next layer
303305
continue
304306
}
@@ -312,6 +314,7 @@ func (r *AppImageInfoController) GetImageInfo(ctx context.Context, refs []string
312314
s := "layer-" + layer.Digest.String()
313315
if s == status.Ref {
314316
imageLayer.Offset = status.Offset
317+
break
315318
}
316319
}
317320
} else {

controllers/tailscale_acl_controller.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ func (r *TailScaleACLController) Reconcile(ctx context.Context, req ctrl.Request
131131
filteredApps = append(filteredApps, app)
132132
}
133133

134+
sort.Slice(filteredApps, func(i, j int) bool {
135+
return filteredApps[j].CreationTimestamp.Before(&filteredApps[i].CreationTimestamp)
136+
})
137+
134138
tailScaleACLConfig := "tailscale-acl"
135139
headScaleNamespace := fmt.Sprintf("user-space-%s", req.Namespace)
136140

@@ -240,8 +244,10 @@ func (r *TailScaleACLController) Reconcile(ctx context.Context, req ctrl.Request
240244
}
241245
headScaleACLMd5 := ""
242246
if deploy.Spec.Template.Annotations != nil {
247+
klog.Infof("headscaleaclmd5..: %s", deploy.Spec.Template.Annotations[tailScaleACLPolicyMd5Key])
243248
headScaleACLMd5 = deploy.Spec.Template.Annotations[tailScaleACLPolicyMd5Key]
244249
}
250+
klog.Infof("oldheadscaleACLmd5: %v, newmd5: %v", headScaleACLMd5, curTailScaleACLPolicyMd5Sum)
245251
if headScaleACLMd5 != curTailScaleACLPolicyMd5Sum {
246252
if deploy.Spec.Template.Annotations == nil {
247253
deploy.Spec.Template.Annotations = make(map[string]string)
@@ -251,6 +257,7 @@ func (r *TailScaleACLController) Reconcile(ctx context.Context, req ctrl.Request
251257
deploy.Spec.Template.Annotations[tailScaleACLPolicyMd5Key] = curTailScaleACLPolicyMd5Sum
252258
err = r.Update(ctx, deploy)
253259
if err != nil {
260+
klog.Errorf("update headscale deploy failed: %v", err)
254261
return ctrl.Result{}, err
255262
}
256263
klog.Infof("rolling update headscale...")

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ require (
182182
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
183183
github.com/morikuni/aec v1.0.0 // indirect
184184
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
185+
github.com/nats-io/nats.go v1.36.0 // indirect
186+
github.com/nats-io/nkeys v0.4.7 // indirect
187+
github.com/nats-io/nuid v1.0.1 // indirect
185188
github.com/opencontainers/runtime-spec v1.2.0 // indirect
186189
github.com/opencontainers/selinux v1.11.1 // indirect
187190
github.com/ostreedev/ostree-go v0.0.0-20210805093236-719684c64e4f // indirect

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,6 +1563,12 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8m
15631563
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
15641564
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
15651565
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
1566+
github.com/nats-io/nats.go v1.36.0 h1:suEUPuWzTSse/XhESwqLxXGuj8vGRuPRoG7MoRN/qyU=
1567+
github.com/nats-io/nats.go v1.36.0/go.mod h1:Ubdu4Nh9exXdSz0RVWRFBbRfrbSxOYd26oF0wkWclB8=
1568+
github.com/nats-io/nkeys v0.4.7 h1:RwNJbbIdYCoClSDNY7QVKZlyb/wfT6ugvFCiKy6vDvI=
1569+
github.com/nats-io/nkeys v0.4.7/go.mod h1:kqXRgRDPlGy7nGaEDMuYzmiJCIAAWDK0IMBtDmGD0nc=
1570+
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
1571+
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
15661572
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
15671573
github.com/nxadm/tail v1.4.8 h1:nPr65rt6Y5JFSKQO7qToXr7pePgD6Gwiw05lkbyAQTE=
15681574
github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+AU=

pkg/apiserver/api/types.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ type InstallationResponse struct {
1313

1414
// InstallationResponseData represents the installation response uid.
1515
type InstallationResponseData struct {
16-
UID string `json:"uid"`
16+
UID string `json:"uid"`
17+
OpID string `json:"opID"`
1718
}
1819

1920
// DependenciesRespData represents the dependencies of an application.

pkg/apiserver/filters.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ func (h *Handler) authenticate(req *restful.Request, resp *restful.Response, cha
9797
"/app-service/v1/runasuser/inject",
9898
"/app-service/v1/terminus/version",
9999
"/app-service/v1/app-label/inject",
100+
"/app-service/v1/apps/image-info",
101+
"/app-service/v1/all/apps",
100102
}
101103

102104
needAuth := true

pkg/apiserver/handler_app.go

Lines changed: 56 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package apiserver
33
import (
44
"context"
55
"encoding/json"
6-
"fmt"
76
"os"
87
"sort"
98
"strconv"
@@ -157,6 +156,7 @@ func (h *Handler) operate(req *restful.Request, resp *restful.Response) {
157156
AppNamespace: am.Spec.AppNamespace,
158157
AppOwner: am.Spec.AppOwner,
159158
OpType: am.Status.OpType,
159+
OpID: am.Status.OpID,
160160
ResourceType: am.Spec.Type.String(),
161161
State: am.Status.State,
162162
Message: am.Status.Message,
@@ -191,6 +191,7 @@ func (h *Handler) appsOperate(req *restful.Request, resp *restful.Response) {
191191
AppOwner: am.Spec.AppOwner,
192192
State: am.Status.State,
193193
OpType: am.Status.OpType,
194+
OpID: am.Status.OpID,
194195
ResourceType: am.Spec.Type.String(),
195196
Message: am.Status.Message,
196197
CreationTimestamp: am.CreationTimestamp,
@@ -239,6 +240,7 @@ func (h *Handler) operateHistory(req *restful.Request, resp *restful.Response) {
239240
ResourceType: am.Spec.Type.String(),
240241
OpRecord: v1alpha1.OpRecord{
241242
OpType: r.OpType,
243+
OpID: r.OpID,
242244
Message: r.Message,
243245
Source: r.Source,
244246
Version: r.Version,
@@ -643,16 +645,16 @@ func (h *Handler) allOperateRecommendHistory(req *restful.Request, resp *restful
643645
}
644646

645647
func (h *Handler) allUsersApps(req *restful.Request, resp *restful.Response) {
646-
owner := req.Attribute(constants.UserContextAttribute).(string)
648+
//owner := req.Attribute(constants.UserContextAttribute).(string)
647649
isSysApp := req.QueryParameter("issysapp")
648650
state := req.QueryParameter("state")
649651

650652
//kClient, _ := utils.GetClient()
651-
role, err := kubesphere.GetUserRole(req.Request.Context(), h.kubeConfig, owner)
652-
if err != nil {
653-
api.HandleError(resp, req, err)
654-
return
655-
}
653+
//role, err := kubesphere.GetUserRole(req.Request.Context(), h.kubeConfig, owner)
654+
//if err != nil {
655+
// api.HandleError(resp, req, err)
656+
// return
657+
//}
656658

657659
ss := make([]string, 0)
658660
if state != "" {
@@ -671,57 +673,54 @@ func (h *Handler) allUsersApps(req *restful.Request, resp *restful.Response) {
671673
}
672674

673675
filteredApps := make([]v1alpha1.Application, 0)
674-
appsMap := make(map[string]v1alpha1.Application)
676+
appsMap := make(map[string]*v1alpha1.Application)
675677
// get pending app's from app managers
676678
ams, err := h.appmgrLister.List(labels.Everything())
677679

678680
for _, am := range ams {
679681
if am.Spec.Type != v1alpha1.App {
680682
continue
681683
}
682-
if role != "platform-admin" && am.Spec.AppOwner != owner {
684+
685+
if !stateSet.Has(am.Status.State.String()) {
686+
continue
687+
}
688+
if len(isSysApp) > 0 && isSysApp == "true" {
689+
continue
690+
}
691+
if userspace.IsSysApp(am.Spec.AppName) {
683692
continue
684693
}
685694

686-
if am.Status.State == v1alpha1.Pending || am.Status.State == v1alpha1.Installing ||
687-
am.Status.State == v1alpha1.Downloading {
688-
if !stateSet.Has(v1alpha1.Pending.String()) || !stateSet.Has(v1alpha1.Installing.String()) ||
689-
!stateSet.Has(v1alpha1.Downloading.String()) {
690-
continue
691-
}
692-
if len(isSysApp) > 0 && isSysApp == "true" {
693-
continue
694-
}
695-
var appconfig appcfg.ApplicationConfig
696-
err = json.Unmarshal([]byte(am.Spec.Config), &appconfig)
697-
if err != nil {
698-
api.HandleError(resp, req, err)
699-
return
700-
}
701-
now := metav1.Now()
702-
app := v1alpha1.Application{
703-
TypeMeta: metav1.TypeMeta{},
704-
ObjectMeta: metav1.ObjectMeta{
705-
Name: am.Name,
706-
CreationTimestamp: am.CreationTimestamp,
707-
},
708-
Spec: v1alpha1.ApplicationSpec{
709-
Name: am.Spec.AppName,
710-
Appid: apputils.GetAppID(am.Spec.AppName),
711-
IsSysApp: userspace.IsSysApp(am.Spec.AppName),
712-
Namespace: am.Spec.AppNamespace,
713-
Owner: am.Spec.AppOwner,
714-
Entrances: appconfig.Entrances,
715-
Icon: appconfig.Icon,
716-
},
717-
Status: v1alpha1.ApplicationStatus{
718-
State: am.Status.State.String(),
719-
StatusTime: &now,
720-
UpdateTime: &now,
721-
},
722-
}
723-
appsMap[fmt.Sprintf("%s-%s", am.Spec.AppName, am.Spec.AppOwner)] = app
695+
var appconfig appcfg.ApplicationConfig
696+
err = json.Unmarshal([]byte(am.Spec.Config), &appconfig)
697+
if err != nil {
698+
api.HandleError(resp, req, err)
699+
return
724700
}
701+
now := metav1.Now()
702+
app := v1alpha1.Application{
703+
TypeMeta: metav1.TypeMeta{},
704+
ObjectMeta: metav1.ObjectMeta{
705+
Name: am.Name,
706+
CreationTimestamp: am.CreationTimestamp,
707+
},
708+
Spec: v1alpha1.ApplicationSpec{
709+
Name: am.Spec.AppName,
710+
Appid: apputils.GetAppID(am.Spec.AppName),
711+
IsSysApp: userspace.IsSysApp(am.Spec.AppName),
712+
Namespace: am.Spec.AppNamespace,
713+
Owner: am.Spec.AppOwner,
714+
Entrances: appconfig.Entrances,
715+
Icon: appconfig.Icon,
716+
},
717+
Status: v1alpha1.ApplicationStatus{
718+
State: am.Status.State.String(),
719+
StatusTime: &now,
720+
UpdateTime: &now,
721+
},
722+
}
723+
appsMap[am.Name] = &app
725724
}
726725

727726
allApps, err := h.appLister.List(labels.Everything())
@@ -735,17 +734,23 @@ func (h *Handler) allUsersApps(req *restful.Request, resp *restful.Response) {
735734
if !stateSet.Has(a.Status.State) {
736735
continue
737736
}
738-
if role != "platform-admin" && a.Spec.Owner != owner {
737+
//if role != "platform-admin" && a.Spec.Owner != owner {
738+
// continue
739+
//}
740+
if len(isSysApp) > 0 && strconv.FormatBool(a.Spec.IsSysApp) != isSysApp {
739741
continue
740742
}
741-
if len(isSysApp) > 0 && strconv.FormatBool(a.Spec.IsSysApp) != isSysApp {
743+
if a.Spec.IsSysApp {
744+
appsMap[a.Name] = a
742745
continue
743746
}
744-
appsMap[fmt.Sprintf("%s-%s", a.Spec.Name, a.Spec.Owner)] = *a
747+
if v, ok := appsMap[a.Name]; ok {
748+
v.Spec.Settings = a.Spec.Settings
749+
}
745750
}
746751

747752
for _, app := range appsMap {
748-
filteredApps = append(filteredApps, app)
753+
filteredApps = append(filteredApps, *app)
749754
}
750755

751756
// sort by create time desc

pkg/apiserver/handler_appupgrade.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ func (h *Handler) appUpgrade(req *restful.Request, resp *restful.Response) {
123123
now := metav1.Now()
124124
status := appv1alpha1.ApplicationManagerStatus{
125125
OpType: appv1alpha1.UpgradeOp,
126+
OpID: a.ResourceVersion,
126127
State: appv1alpha1.Upgrading,
127128
Message: "waiting for upgrade",
128129
Payload: map[string]string{
@@ -135,13 +136,15 @@ func (h *Handler) appUpgrade(req *restful.Request, resp *restful.Response) {
135136
UpdateTime: &now,
136137
}
137138

138-
_, err = apputils.UpdateAppMgrStatus(appMgrName, status)
139+
am, err := apputils.UpdateAppMgrStatus(appMgrName, status)
139140
if err != nil {
140141
api.HandleError(resp, req, err)
141142
return
142143
}
144+
utils.PublishAsync(fmt.Sprintf("os.application.%s", am.Spec.AppOwner), am.Spec.AppName, appv1alpha1.Upgrading, am.Status)
145+
143146
resp.WriteEntity(api.InstallationResponse{
144147
Response: api.Response{Code: 200},
145-
Data: api.InstallationResponseData{UID: app},
148+
Data: api.InstallationResponseData{UID: app, OpID: status.OpID},
146149
})
147150
}

0 commit comments

Comments
 (0)