Skip to content

Commit

Permalink
add secret operation
Browse files Browse the repository at this point in the history
  • Loading branch information
zc2638 committed Sep 7, 2023
1 parent ada6c5d commit 86e21b4
Show file tree
Hide file tree
Showing 9 changed files with 371 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Controllable CICD service
- Support docker image pull auth for registry.
- Support secret.
- Add more storage backend support, like MySQL and Postgres.
- Support setting mode.

## Setup

Expand Down
52 changes: 52 additions & 0 deletions core/clients/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ type Server interface {
}

type ServerV1 interface {
SecretList(ctx context.Context) ([]*v1.Secret, error)
SecretInfo(ctx context.Context, namespace, name string) (*v1.Secret, error)
SecretCreate(ctx context.Context, data *v1.Secret) error
SecretUpdate(ctx context.Context, data *v1.Secret) error
SecretDelete(ctx context.Context, namespace, name string) error

WorkflowList(ctx context.Context, page v1.Pagination) ([]*v1.Workflow, *v1.Pagination, error)
WorkflowInfo(ctx context.Context, namespace, name string) (*v1.Workflow, error)
WorkflowCreate(ctx context.Context, data *v1.Workflow) error
Expand Down Expand Up @@ -80,6 +86,52 @@ func (c *serverV1) R(ctx context.Context) *resty.Request {
return c.rc.R().SetContext(ctx)
}

func (c *serverV1) SecretList(ctx context.Context) ([]*v1.Secret, error) {
var result []*v1.Secret
req := c.R(ctx).SetResult(&result)
resp, err := req.Get("/secret")
if err := handleClientError(resp, err); err != nil {
return nil, err
}
return result, nil
}

func (c *serverV1) SecretInfo(ctx context.Context, namespace, name string) (*v1.Secret, error) {
var result v1.Secret
req := c.R(ctx).
SetPathParam("namespace", namespace).
SetPathParam("name", name).
SetResult(&result)
resp, err := req.Get("/secret/{namespace}/{name}")
if err := handleClientError(resp, err); err != nil {
return nil, err
}
return &result, nil
}

func (c *serverV1) SecretCreate(ctx context.Context, data *v1.Secret) error {
req := c.R(ctx).SetBody(data)
resp, err := req.Post("/secret")
return handleClientError(resp, err)
}

func (c *serverV1) SecretUpdate(ctx context.Context, data *v1.Secret) error {
req := c.R(ctx).
SetBody(data).
SetPathParam("namespace", data.GetNamespace()).
SetPathParam("name", data.GetName())
resp, err := req.Put("/secret/{namespace}/{name}")
return handleClientError(resp, err)
}

func (c *serverV1) SecretDelete(ctx context.Context, namespace, name string) error {
req := c.R(ctx).
SetPathParam("namespace", namespace).
SetPathParam("name", name)
resp, err := req.Delete("/secret/{namespace}/{name}")
return handleClientError(resp, err)
}

func (c *serverV1) WorkflowList(ctx context.Context, page v1.Pagination) ([]*v1.Workflow, *v1.Pagination, error) {
type resultT struct {
v1.Pagination
Expand Down
61 changes: 60 additions & 1 deletion core/command/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func NewCtl() *cobra.Command {
"that contains the configuration to exec"),
)

secretCmd := &cobra.Command{Use: "secret", Short: "secret operation"}
Register(secretCmd, "list", "list secrets", secretList)
Register(secretCmd, "delete", "delete secret", secretDelete)

workflowCmd := &cobra.Command{Use: "workflow", Short: "workflow operation"}
Register(workflowCmd, "get", "get workflow info", workflowGet, workflowGetExample)
Register(workflowCmd, "list", "list workflows", workflowList, workflowListExample)
Expand All @@ -79,6 +83,43 @@ func NewCtl() *cobra.Command {
return cmd
}

func secretList(cmd *cobra.Command, _ []string) error {
sc, err := newServerClient(cmd)
if err != nil {
return err
}
result, err := sc.SecretList(context.Background())
if err != nil {
return err
}

if len(result) == 0 {
writeString("No resources found.")
return nil
}

t := printer.NewTab("NAMESPACE", "NAME", "AGE")
for _, v := range result {
since := time.Since(v.Creation).Round(time.Second)
t.Add(v.GetNamespace(), v.GetName(), since.String())
}
t.Print()
return nil
}

func secretDelete(cmd *cobra.Command, args []string) error {
namespace, name, err := getNN(args)
if err != nil {
return err
}

sc, err := newServerClient(cmd)
if err != nil {
return err
}
return sc.SecretDelete(context.Background(), namespace, name)
}

func workflowGet(cmd *cobra.Command, args []string) error {
namespace, name, err := getNN(args)
if err != nil {
Expand Down Expand Up @@ -319,7 +360,25 @@ func apply(cmd *cobra.Command, _ []string) error {

ctx := context.Background()

// TODO kind Secret
for _, obj := range objSet[v1.KindSecret] {
var data v1.Secret
if err := obj.ToObject(&data); err != nil {
return err
}
_, err := sc.SecretInfo(ctx, data.GetNamespace(), data.GetName())
if err == nil {
if err = sc.SecretUpdate(ctx, &data); err == nil {
writeString(fmt.Sprintf("Update: %s", data.Metadata.String()))
}
} else if errors.Is(err, constant.ErrNoRecord) {
if err = sc.SecretCreate(ctx, &data); err == nil {
writeString(fmt.Sprintf("Create: %s", data.Metadata.String()))
}
}
if err != nil {
return err
}
}
for _, obj := range objSet[v1.KindWorkflow] {
var data v1.Workflow
if err := obj.ToObject(&data); err != nil {
Expand Down
16 changes: 16 additions & 0 deletions core/command/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ func (s Example) String() string {
return string(s)
}

const secretListExample Example = `
# List secrets
inkctl secret list
`

const secretDeleteExample Example = `
# Definition
inkctl secret delete {namespace}/{name}
# Delete a secret
inkctl secret delete default/test
# Delete secret with default namespace
inkctl secret delete test
`

const workflowListExample Example = `
# List workflows (default size: 10)
inkctl workflow list
Expand Down
12 changes: 12 additions & 0 deletions core/handler/server/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/zc2638/ink/core/service/box"
"github.com/zc2638/ink/core/service/build"
"github.com/zc2638/ink/core/service/secret"
"github.com/zc2638/ink/core/service/workflow"
)

Expand All @@ -31,6 +32,7 @@ func Handler(middlewares chi.Middlewares) http.Handler {
workflowSrv := workflow.New()
boxSrv := box.New()
buildSrv := build.New()
secretSrv := secret.New()

r.Route("/box", func(r chi.Router) {
r.Get("/", boxList(boxSrv))
Expand Down Expand Up @@ -64,5 +66,15 @@ func Handler(middlewares chi.Middlewares) http.Handler {
r.Delete("/", workflowDelete(workflowSrv))
})
})

r.Route("/secret", func(r chi.Router) {
r.Get("/", secretList(secretSrv))
r.Post("/", secretCreate(secretSrv))
r.Route("/{namespace}/{name}", func(r chi.Router) {
r.Get("/", secretInfo(secretSrv))
r.Put("/", secretUpdate(secretSrv))
r.Delete("/", secretDelete(secretSrv))
})
})
return r
}
101 changes: 101 additions & 0 deletions core/handler/server/secret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright © 2023 zc2638 <zc2638@qq.com>.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package server

import (
"encoding/json"
"net/http"

"github.com/99nil/gopkg/ctr"

"github.com/zc2638/ink/core/handler/wrapper"
"github.com/zc2638/ink/core/service"
v1 "github.com/zc2638/ink/pkg/api/core/v1"
)

func secretList(secretSrv service.Secret) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
result, err := secretSrv.List(r.Context(), "")
if err != nil {
wrapper.InternalError(w, err)
return
}
ctr.OK(w, result)
}
}

func secretInfo(secretSrv service.Secret) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
namespace := wrapper.URLParam(r, "namespace")
name := wrapper.URLParam(r, "name")

result, err := secretSrv.Info(r.Context(), namespace, name)
if err != nil {
wrapper.InternalError(w, err)
return
}
ctr.OK(w, result)
}
}

func secretCreate(secretSrv service.Secret) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var in v1.Secret
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
wrapper.BadRequest(w, err)
return
}

if err := secretSrv.Create(r.Context(), &in); err != nil {
wrapper.InternalError(w, err)
return
}
ctr.Success(w)
}
}

func secretUpdate(secretSrv service.Secret) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
namespace := wrapper.URLParam(r, "namespace")
name := wrapper.URLParam(r, "name")

var in v1.Secret
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
wrapper.BadRequest(w, err)
return
}
in.SetNamespace(namespace)
in.SetName(name)

if err := secretSrv.Update(r.Context(), &in); err != nil {
wrapper.InternalError(w, err)
return
}
ctr.Success(w)
}
}

func secretDelete(secretSrv service.Secret) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
namespace := wrapper.URLParam(r, "namespace")
name := wrapper.URLParam(r, "name")

if err := secretSrv.Delete(r.Context(), namespace, name); err != nil {
wrapper.InternalError(w, err)
return
}
ctr.Success(w)
}
}
Loading

0 comments on commit 86e21b4

Please sign in to comment.