Skip to content

Commit

Permalink
feat(addon): addon API support
Browse files Browse the repository at this point in the history
  • Loading branch information
JacieChao committed Aug 21, 2023
1 parent eb58e3a commit b31cd96
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/common/addon.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

type Addon struct {
Name string `json:"name" gorm:"primaryKey;not null" wrangler:"required"`
Name string `json:"name" gorm:"primaryKey;not null" wrangler:"required,noupdate"`
Description string `json:"description,omitempty"`
Manifest []byte `json:"manifest" gorm:"type:bytes" wrangler:"required"`
Values types.StringMap `json:"values,omitempty" gorm:"type:stringMap"`
Expand Down
10 changes: 10 additions & 0 deletions pkg/server/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/cnrancher/autok3s/pkg/common"
"github.com/cnrancher/autok3s/pkg/server/store/addon"
"github.com/cnrancher/autok3s/pkg/server/store/cluster"
"github.com/cnrancher/autok3s/pkg/server/store/credential"
"github.com/cnrancher/autok3s/pkg/server/store/explorer"
Expand Down Expand Up @@ -152,3 +153,12 @@ func initSSHKey(s *types.APISchemas) {
}
})
}

func initAddon(s *types.APISchemas) {
s.MustImportAndCustomize(common.Addon{}, func(schema *types.APISchema) {
schema.Store = &addon.Store{}
schema.CollectionMethods = []string{http.MethodPost, http.MethodGet}
schema.ResourceMethods = []string{http.MethodPut, http.MethodGet, http.MethodDelete}
})

}
1 change: 1 addition & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func Start() http.Handler {
initSettings(s.Schemas)
initPackage(s.Schemas)
initSSHKey(s.Schemas)
initAddon(s.Schemas)

apiroot.Register(s.Schemas, []string{"v1"})
router := mux.NewRouter()
Expand Down
136 changes: 136 additions & 0 deletions pkg/server/store/addon/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package addon

import (
"bytes"
"errors"
"reflect"

"github.com/cnrancher/autok3s/pkg/common"
"github.com/rancher/apiserver/pkg/store/empty"
"github.com/rancher/apiserver/pkg/types"
"github.com/rancher/wrangler/pkg/data/convert"
"github.com/sirupsen/logrus"
)

type Store struct {
empty.Store
}

func (a *Store) Create(apiOp *types.APIRequest, schema *types.APISchema, data types.APIObject) (types.APIObject, error) {
input := &common.Addon{}
err := convert.ToObj(data.Data(), input)
if err != nil {
return types.APIObject{}, err
}

// validate creation
if err := common.ValidateName(input.Name); err != nil {
return types.APIObject{}, err
}
if input.Manifest == nil {
return types.APIObject{}, errors.New("manifest file or manifest file content cannot be empty")
}

manifest, err := common.GetManifest("", string(input.Manifest))
if err != nil {
return types.APIObject{}, err
}

addon := &common.Addon{
Name: input.Name,
Description: input.Description,
Manifest: []byte(manifest),
Values: input.Values,
}
err = common.DefaultDB.SaveAddon(addon)
if err != nil {
return types.APIObject{}, err
}
return a.ByID(apiOp, schema, addon.Name)
}

func (a *Store) List(apiOp *types.APIRequest, schema *types.APISchema) (types.APIObjectList, error) {
result := types.APIObjectList{}
list, err := common.DefaultDB.ListAddon()
if err != nil {
return result, err
}

for _, addon := range list {
result.Objects = append(result.Objects, types.APIObject{
ID: addon.Name,
Type: schema.ID,
Object: addon,
})
}

return result, nil
}

func (a *Store) ByID(apiOp *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) {
addon, err := common.DefaultDB.GetAddon(id)
if err != nil {
return types.APIObject{}, err
}
return types.APIObject{
ID: addon.Name,
Type: schema.ID,
Object: addon,
}, nil
}

func (a *Store) Update(apiOp *types.APIRequest, schema *types.APISchema, data types.APIObject, id string) (types.APIObject, error) {
input := &common.Addon{}
err := convert.ToObj(data.Data(), input)
if err != nil {
return types.APIObject{}, err
}

addon, err := common.DefaultDB.GetAddon(id)
if err != nil {
return types.APIObject{}, err
}

manifestContent := addon.Manifest
if input.Manifest != nil {
manifestContent = input.Manifest
}

manifestString, err := common.GetManifest("", string(manifestContent))
if err != nil {
return types.APIObject{}, err
}
manifest := []byte(manifestString)

isChanged := false
if input.Description != "" && input.Description != addon.Description {
addon.Description = input.Description
isChanged = true
}

if !bytes.Equal(manifest, addon.Manifest) {
addon.Manifest = manifest
isChanged = true
}

if !reflect.DeepEqual(input.Values, addon.Values) {
addon.Values = input.Values
isChanged = true
}

if isChanged {
if err := common.DefaultDB.SaveAddon(addon); err != nil {
logrus.Fatalln(err)
}
}
return a.ByID(apiOp, schema, id)
}

func (a *Store) Delete(apiOp *types.APIRequest, schema *types.APISchema, id string) (types.APIObject, error) {
err := common.DefaultDB.DeleteAddon(id)
return types.APIObject{}, err
}

func (a *Store) Watch(apiOp *types.APIRequest, schema *types.APISchema, wr types.WatchRequest) (chan types.APIEvent, error) {
return common.DefaultDB.Watch(apiOp, schema), nil
}

0 comments on commit b31cd96

Please sign in to comment.