generated from LinuxSuRen/.github
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support to update argo cd application
- Loading branch information
1 parent
3688ad9
commit 6c14882
Showing
10 changed files
with
741 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.