Skip to content

Commit

Permalink
Merge pull request #1 from sixwaaaay/main
Browse files Browse the repository at this point in the history
support studio command
  • Loading branch information
kqzh authored Aug 7, 2023
2 parents a9ead41 + 35affda commit a22fe72
Show file tree
Hide file tree
Showing 12 changed files with 1,125 additions and 1 deletion.
43 changes: 43 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: "CI"
on:
push:
branches:
- 'main'
tags:
- ''
pull_request:
branches:
- '*'
jobs:

test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'

- name: golangci-lint
run:
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.53.3
golangci-lint run ./... --out-format=github-actions
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}


- name: Install minikube
run: |
curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_latest_amd64.deb
sudo dpkg -i minikube_latest_amd64.deb
- name: Set up minikube environment
run: |
minikube start
- name: Go Code Test
run: |
go mod tidy
go test ./... -race
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
### Go template

# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# operator-cli
# ngctl
37 changes: 37 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2023 The nebula-contrib 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.
* 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 cmd

import (
"path/filepath"

"github.com/spf13/cobra"
"k8s.io/client-go/util/homedir"
)

// Flag Values for RootCmd
var (
kubeConfig string
)

var RootCmd = &cobra.Command{
Use: "ngctl [command]",
Short: "the command line tool for nebula operator",
Long: "ngctl is the command line tool for nebula operator.",
}

func init() {
RootCmd.PersistentFlags().StringVar(&kubeConfig, "kubeconfig", filepath.Join(homedir.HomeDir(), ".kube", "config"), "path of the kubernetes config file")
RootCmd.AddCommand(studioCmd())
}
121 changes: 121 additions & 0 deletions cmd/studio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2023 The nebula-contrib 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.
* 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 cmd

import (
"context"

"github.com/spf13/cobra"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"

"github.com/nebula-contrib/ngctl/pkg/studio"
"github.com/nebula-contrib/ngctl/pkg/util"
)

const studioLabelKey = "ngctl/nebula-studio"

func studioCmd() *cobra.Command {
var (
name string
namespace string
nodePort int32
image string
)
cmd := &cobra.Command{
Use: "studio",
Short: "the command line tool for nebula graph studio",
Long: "studio is a command line tool for nebula graph studio.",
Example: ` # installStudio nebula graph studio
ngctl studio installStudio --name studio --nodePort 30180
# uninstall nebula graph studio
ngctl studio uninstall --name studio
`,
}
cmd.PersistentFlags().StringVar(&name, "name", "studio", "name of the nebula graph studio")
cmd.PersistentFlags().StringVar(&namespace, "namespace", "default", "namespace of the nebula graph studio")

install := cobra.Command{
Use: "install",
Short: "install nebula graph studio",
Long: "install nebula graph studio.",
RunE: func(cmd *cobra.Command, args []string) error {
return installStudio(name, namespace, image, nodePort)
},
}
install.PersistentFlags().Int32Var(&nodePort, "nodePort", 30180, "nodePort of the nebula graph studio")
install.PersistentFlags().StringVar(&image, "image", "vesoft/nebula-graph-studio:v3.7.0", "image of the nebula graph studio")

cmd.AddCommand(&install)

uninstall := cobra.Command{
Use: "uninstall",
Short: "uninstall nebula graph studio",
Long: "uninstall nebula graph studio.",
RunE: func(cmd *cobra.Command, args []string) error {
return uninstallStudio(name, namespace)
},
}
cmd.AddCommand(&uninstall)

return cmd
}

func installStudio(name, namespace string, image string, nodePort int32) error {
clientSet, err := util.NewClientSet(kubeConfig)
if err != nil {
return err
}
labels := map[string]string{
studioLabelKey: name,
}
var ctx = context.Background()

deploy := studio.CreateDeployment(name, namespace, labels, 1, image)
deployInterface := clientSet.AppsV1().Deployments(namespace)
err = util.CreateResource[appsv1.Deployment](ctx, deployInterface, deploy, name, studioLabelKey)
if err != nil {
return err
}

svc := studio.CreateService(name, namespace, labels, nodePort)
svcInterface := clientSet.CoreV1().Services(namespace)
err = util.CreateResource[corev1.Service](ctx, svcInterface, svc, name, studioLabelKey)
if err != nil {
return err
}
return nil
}

func uninstallStudio(name, namespace string) error {
clientSet, err := util.NewClientSet(kubeConfig)
if err != nil {
return err
}
var ctx = context.Background()

// remove deploy
deployments := clientSet.AppsV1().Deployments(namespace)
err = util.RemoveResource[appsv1.Deployment](ctx, deployments, name, studioLabelKey)
if err != nil {
return err
}
// remove service
services := clientSet.CoreV1().Services(namespace)
err = util.RemoveResource[corev1.Service](ctx, services, name, studioLabelKey)
if err != nil {
return err
}
return nil
}
51 changes: 51 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module github.com/nebula-contrib/ngctl

go 1.20

require (
github.com/spf13/cobra v1.7.0
k8s.io/api v0.27.3
k8s.io/apimachinery v0.27.3
k8s.io/client-go v0.27.3
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.1 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/term v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit a22fe72

Please sign in to comment.