Skip to content

Commit

Permalink
Merge pull request #256 from LinuxSuRen/ks-app
Browse files Browse the repository at this point in the history
Support to update argo cd application
  • Loading branch information
ks-ci-bot committed Mar 4, 2022
2 parents 3688ad9 + 6c14882 commit fd5307c
Show file tree
Hide file tree
Showing 10 changed files with 741 additions and 3 deletions.
42 changes: 42 additions & 0 deletions docs/app.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
The purpose of the sub-command `ks app` is to update the Argo CD Application git repository.

This command will do the following steps:

* Clone the target git repository
* Update the kustomization YAML file\
* Commit and push the changes to target branch

## Restriction
Only support kustomization for now.

## Usage

Use git username and password directly:
```shell
ks app update --app-name app --app-namespace default \
--name good --newName good-new \
--git-password glpat-ULXLsjmC1t6zzFFHtBsD --git-username=linuxsuren1 \
--git-target-branch test
```

Take the git auth from a Kubernetes Secret:
```shell
ks app update --app-name app --app-namespace default \
--name good --newName good-new1 \
--secret-namespace default --secret-name gitlab
```

## Supported Secrets

Basic auth type of Secret:
```yaml
apiVersion: v1
kind: Secret
type: "kubernetes.io/basic-auth"
metadata:
creationTimestamp: null
name: github
stringData:
username: linuxsuren
password: token-or-password
```
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ require (
github.com/Pallinder/go-randomdata v1.2.0
github.com/evanphx/json-patch v4.9.0+incompatible
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1
github.com/go-git/go-git/v5 v5.4.2
github.com/huandu/xstrings v1.3.2 // indirect
github.com/jenkins-x/go-scm v1.11.1
github.com/linuxsuren/cobra-extension v0.0.11
github.com/linuxsuren/go-cli-alias v0.0.8
github.com/linuxsuren/http-downloader v0.0.35
Expand All @@ -20,9 +22,11 @@ require (
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/src-d/go-git.v4 v4.13.1
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
k8s.io/api v0.19.4
k8s.io/apimachinery v0.19.4
Expand Down
37 changes: 37 additions & 0 deletions go.sum

Large diffs are not rendered by default.

120 changes: 120 additions & 0 deletions kubectl-plugin/app/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*
Copyright 2022 The KubeSphere Authors.
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 app

import (
"context"
"errors"
"fmt"
goscm "github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/go-scm/scm/driver/github"
"github.com/jenkins-x/go-scm/scm/driver/gitlab"
"github.com/jenkins-x/go-scm/scm/transport/oauth2"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"net/http"
)

// ClientFactory responsible for creating a git client
type ClientFactory struct {
provider string
secretRef *v1.SecretReference
k8sClient ResourceGetter
token string
}

// NewClientFactory creates an instance of the ClientFactory
func NewClientFactory(provider string, token string, secretRef *v1.SecretReference, k8sClient ResourceGetter) *ClientFactory {
return &ClientFactory{
provider: provider,
secretRef: secretRef,
k8sClient: k8sClient,
token: token,
}
}

// GetClient returns the git client with auth
func (c *ClientFactory) GetClient() (client *goscm.Client, err error) {
switch c.provider {
case "github":
client = github.NewDefault()
case "gitlab":
client = gitlab.NewDefault()
default:
err = errors.New("not support git provider: " + c.provider)
return
}

var token string
if token, err = c.getToken(); err != nil {
return
}

client.Client = &http.Client{
Transport: &oauth2.Transport{
Source: oauth2.StaticTokenSource(
&goscm.Token{
Token: token,
},
),
},
}
return
}

func (c *ClientFactory) getToken() (token string, err error) {
if c.token != "" {
token = c.token
} else if c.secretRef != nil {
token, err = c.getTokenFromSecret(c.secretRef)
}
return
}

func (c *ClientFactory) getTokenFromSecret(secretRef *v1.SecretReference) (token string, err error) {
var gitSecret *v1.Secret
if gitSecret, err = c.getSecret(secretRef); err != nil {
return
}

switch gitSecret.Type {
case v1.SecretTypeBasicAuth:
token = string(gitSecret.Data[v1.BasicAuthPasswordKey])
case v1.SecretTypeOpaque:
token = string(gitSecret.Data[v1.ServiceAccountTokenKey])
}
return
}

// getSecret returns the secret, taking the namespace from GitRepository if it is empty
func (c *ClientFactory) getSecret(ref *v1.SecretReference) (secret *v1.Secret, err error) {
secret = &v1.Secret{}
ns := ref.Namespace

if err = c.k8sClient.Get(context.TODO(), types.NamespacedName{
Namespace: ns, Name: ref.Name,
}, secret); err != nil {
err = fmt.Errorf("cannot get secret %v, error is: %v", secret, err)
}
return
}

// ResourceGetter represent the interface for getting Kubernetes resource
type ResourceGetter interface {
Get(ctx context.Context, key types.NamespacedName, obj runtime.Object) error
}
17 changes: 17 additions & 0 deletions kubectl-plugin/app/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package app

import (
"github.com/spf13/cobra"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
)

func NewAppCmd(client dynamic.Interface, clientset *kubernetes.Clientset) (cmd *cobra.Command) {
cmd = &cobra.Command{
Use: "app",
Short: "Manage applications as the GitOps way",
}

cmd.AddCommand(newUpdateCmd(client, clientset))
return
}
Loading

0 comments on commit fd5307c

Please sign in to comment.