Skip to content

Commit

Permalink
Merge pull request #108 from iawia002/dep
Browse files Browse the repository at this point in the history
Remove extra dependencies
  • Loading branch information
iawia002 authored Nov 27, 2024
2 parents c5e55e9 + 9cc5177 commit 1ef7b83
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
14 changes: 4 additions & 10 deletions cmd/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@ import (
"os"
"path"
"path/filepath"
"sigs.k8s.io/yaml"
"strings"

"github.com/iawia002/lia/kubernetes/client"
"github.com/iawia002/lia/kubernetes/client/generic"
"github.com/spf13/cobra"
"kubesphere.io/client-go/kubesphere/scheme"
"sigs.k8s.io/yaml"

"github.com/kubesphere/ksbuilder/pkg/api"
"github.com/kubesphere/ksbuilder/pkg/extension"
"github.com/kubesphere/ksbuilder/pkg/utils"
)

type publishOptions struct {
Expand Down Expand Up @@ -95,17 +93,13 @@ func (o *publishOptions) publish(_ *cobra.Command, args []string) error {
homeDir, _ := os.UserHomeDir()
o.kubeconfig = fmt.Sprintf("%s/.kube/config", homeDir)
}
config, err := client.BuildConfigFromFlags("", o.kubeconfig, client.SetQPS(25, 50))
if err != nil {
return err
}
genericClient, err := generic.NewClient(config, generic.WithScheme(scheme.Scheme), generic.WithCacheReader(false))
genericClient, err := utils.BuildClientFromFlags(o.kubeconfig)
if err != nil {
return err
}
for _, obj := range ext.ToKubernetesResources() {
fmt.Printf("creating %s %s\n", obj.GetObjectKind().GroupVersionKind().Kind, obj.GetName())
if err = client.Apply(context.Background(), genericClient, obj, client.WithFieldManager("ksbuilder")); err != nil {
if err = utils.Apply(context.Background(), genericClient, obj); err != nil {
return err
}
}
Expand Down
11 changes: 3 additions & 8 deletions cmd/unpublish.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import (
"fmt"
"os"

"github.com/iawia002/lia/kubernetes/client"
"github.com/iawia002/lia/kubernetes/client/generic"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1alpha1 "kubesphere.io/api/core/v1alpha1"
"kubesphere.io/client-go/kubesphere/scheme"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"

"github.com/kubesphere/ksbuilder/pkg/utils"
)

type unpublishOptions struct {
Expand Down Expand Up @@ -45,11 +44,7 @@ func (o *unpublishOptions) unpublish(_ *cobra.Command, args []string) error {
homeDir, _ := os.UserHomeDir()
o.kubeconfig = fmt.Sprintf("%s/.kube/config", homeDir)
}
config, err := client.BuildConfigFromFlags("", o.kubeconfig, client.SetQPS(25, 50))
if err != nil {
return err
}
genericClient, err := generic.NewClient(config, generic.WithScheme(scheme.Scheme), generic.WithCacheReader(false))
genericClient, err := utils.BuildClientFromFlags(o.kubeconfig)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ require (
github.com/Masterminds/sprig/v3 v3.2.3
github.com/asaskevich/govalidator v0.0.0-20200428143746-21a406dcc535
github.com/go-playground/validator/v10 v10.14.1
github.com/iawia002/lia v0.5.1
github.com/jedib0t/go-pretty/v6 v6.5.9
github.com/manifoldco/promptui v0.9.0
github.com/mholt/archiver/v3 v3.5.1
Expand Down Expand Up @@ -138,7 +137,7 @@ require (
go.opentelemetry.io/otel/trace v1.19.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
golang.org/x/crypto v0.19.0 // indirect
golang.org/x/exp v0.0.0-20240213143201-ec583247a57a // indirect
golang.org/x/mod v0.15.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/oauth2 v0.17.0 // indirect
golang.org/x/sync v0.5.0 // indirect
Expand Down
6 changes: 0 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@ github.com/foxcpp/go-mockdns v1.0.0/go.mod h1:lgRN6+KxQBawyIghpnl5CezHFGS9VLzvtV
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/ghodss/yaml v0.0.0-20150909031657-73d445a93680/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
Expand Down Expand Up @@ -362,8 +360,6 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO
github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/huandu/xstrings v1.4.0 h1:D17IlohoQq4UcpqD7fDk80P7l+lwAmlFaBHgOipl2FU=
github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/iawia002/lia v0.5.1 h1:SOLEZ1U7SCdaEdG898UHA61E3wbydup2z+YkeBRVAXc=
github.com/iawia002/lia v0.5.1/go.mod h1:rHclN9KlfFOSriFZuDew70WPP7Z5/0oCUzJxEm3BduM=
github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
Expand Down Expand Up @@ -840,8 +836,6 @@ golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gomodules.xyz/jsonpatch/v2 v2.0.1/go.mod h1:IhYNNY4jnS53ZnfE4PAmpKtDpTCj1JFXc+3mwe7XcUU=
gomodules.xyz/jsonpatch/v2 v2.4.0 h1:Ci3iUJyx9UeRx7CeFN8ARgGbkESwJK+KB9lLcWxY/Zw=
gomodules.xyz/jsonpatch/v2 v2.4.0/go.mod h1:AH3dM2RI6uoBZxn3LVrfvJ3E0/9dG4cSrbuBJT4moAY=
gonum.org/v1/gonum v0.0.0-20190331200053-3d26580ed485/go.mod h1:2ltnJ7xHfj0zHS40VVPYEAAMTa3ZGguvHGBSJeRWqE0=
gonum.org/v1/netlib v0.0.0-20190313105609-8cb42192e0e0/go.mod h1:wa6Ws7BG/ESfp6dHfk7C6KdzKA7wR7u/rKwOGE66zvw=
gonum.org/v1/netlib v0.0.0-20190331212654-76723241ea4e/go.mod h1:kS+toOQn6AQKjmKJ7gzohV1XkqsFehRA2FbsbkopSuQ=
Expand Down
40 changes: 40 additions & 0 deletions pkg/utils/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package utils

import (
"context"

"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/client-go/tools/clientcmd"
"kubesphere.io/client-go/kubesphere/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func BuildClientFromFlags(kubeConfigPath string) (client.Client, error) {
restConfig, err := clientcmd.BuildConfigFromFlags("", kubeConfigPath)
if err != nil {
return nil, err
}
return client.New(restConfig, client.Options{
Scheme: scheme.Scheme,
})
}

func Apply(ctx context.Context, c client.Client, obj client.Object) error {
key := client.ObjectKeyFromObject(obj)
newObj := obj.DeepCopyObject().(client.Object)
if err := c.Get(ctx, key, newObj); err != nil {
if !errors.IsNotFound(err) {
return err
}
createOptions := []client.CreateOption{
client.FieldOwner("ksbuilder"),
}
return c.Create(ctx, obj, createOptions...)
}

patchOptions := []client.PatchOption{
client.FieldOwner("ksbuilder"),
client.ForceOwnership,
}
return c.Patch(ctx, obj, client.Apply, patchOptions...)
}

0 comments on commit 1ef7b83

Please sign in to comment.