-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from chaitin/feat/kubernetes
feat: kubernetes
- Loading branch information
Showing
10 changed files
with
853 additions
and
210 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
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,50 @@ | ||
package cmd | ||
|
||
import ( | ||
"context" | ||
|
||
api "github.com/chaitin/libveinmind/go" | ||
"github.com/chaitin/libveinmind/go/plugin" | ||
) | ||
|
||
// ScanClusters scans cluster list. | ||
func ScanClusters( | ||
ctx context.Context, rang plugin.ExecRange, | ||
cluster []api.Cluster, opts ...plugin.ExecOption, | ||
) error { | ||
iter, err := plugin.IterateTyped(rang, "cluster") | ||
if err != nil { | ||
return err | ||
} | ||
return Scan(ctx, iter, cluster, | ||
plugin.WithExecOptions(opts...)) | ||
} | ||
|
||
// ClusterHandler is the handler for specified cluster. | ||
type ClusterHandler func(*Command, api.Cluster) error | ||
|
||
// MapClusterCommand issues defaultIndex.MapClusterCommand. | ||
func MapClusterCommand( | ||
c *Command, f ClusterHandler, | ||
) *Command { | ||
return defaultIndex.MapClusterCommand(c, f) | ||
} | ||
|
||
// MapClusterCommand attempts to create a cluster command. | ||
// | ||
// The command will attempt to initialize the cluster object | ||
// from specified mode with flags, and then invoke the function | ||
// specified by caller. | ||
func (idx *Index) MapClusterCommand( | ||
c *Command, f ClusterHandler, | ||
) *Command { | ||
return idx.MapModeCommand(c, "cluster", struct{}{}, func( | ||
c *Command, _ []string, root interface{}, | ||
) error { | ||
r, ok := root.(api.Cluster) | ||
if !ok { | ||
return IncompatibleMode() | ||
} | ||
return f(c, r) | ||
}) | ||
} |
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,87 @@ | ||
package cmd | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/spf13/pflag" | ||
|
||
"github.com/chaitin/libveinmind/go/kubernetes" | ||
"github.com/chaitin/libveinmind/go/pkg/pflagext" | ||
"github.com/chaitin/libveinmind/go/plugin" | ||
) | ||
|
||
type kubernetesRoot struct { | ||
k *kubernetes.Kubernetes | ||
} | ||
|
||
func (r kubernetesRoot) ID() interface{} { | ||
return r.k | ||
} | ||
|
||
func (r kubernetesRoot) Mode() string { | ||
return "kubernetes" | ||
} | ||
|
||
func (r kubernetesRoot) Options() plugin.ExecOption { | ||
return plugin.WithExecOptions( | ||
plugin.WithPrependArgs( | ||
"--kube-config", r.k.ConfigPath()), | ||
plugin.WithPrependArgs( | ||
"--namespace", r.k.CurrentNamespace()), | ||
plugin.WithPrependArgs( | ||
"--in-cluster", func() string { | ||
if r.k.InCluster() { | ||
return "true" | ||
} else { | ||
return "false" | ||
} | ||
}())) | ||
} | ||
|
||
var kubernetesFlags []kubernetes.NewOption | ||
|
||
type kubernetesMode struct { | ||
} | ||
|
||
func (kubernetesMode) Name() string { | ||
return "kubernetes" | ||
} | ||
|
||
func (kubernetesMode) AddFlags(fset *pflag.FlagSet) { | ||
pflagext.StringVarF(fset, func(path string) error { | ||
kubernetesFlags = append(kubernetesFlags, | ||
kubernetes.WithKubeConfig(path)) | ||
return nil | ||
}, "kube-config", | ||
`flag "--kube-config" specified kube config`) | ||
pflagext.StringVarF(fset, func(namespace string) error { | ||
kubernetesFlags = append(kubernetesFlags, | ||
kubernetes.WithNamespace(namespace)) | ||
return nil | ||
}, "namespace", | ||
`flag "--namespace" specified namespace`) | ||
pflagext.StringVarF(fset, func(inCluster string) error { | ||
if strings.ToLower(inCluster) == "true" { | ||
kubernetesFlags = append(kubernetesFlags, | ||
kubernetes.WithInCluster()) | ||
} | ||
return nil | ||
}, "in-cluster", | ||
`flag "--in-cluster" specified in-cluster`) | ||
} | ||
|
||
func (kubernetesMode) Invoke(c *Command, args []string, m ModeHandler) error { | ||
k, err := kubernetes.New(kubernetesFlags...) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { _ = k.Close() }() | ||
return m(c, args, k) | ||
} | ||
|
||
func init() { | ||
RegisterPartition(func(k *kubernetes.Kubernetes) Root { | ||
return kubernetesRoot{k: k} | ||
}) | ||
RegisterMode(&kubernetesMode{}) | ||
} |
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,94 @@ | ||
package kubernetes | ||
|
||
type NamespaceKind string | ||
type ClusterKind string | ||
|
||
const ( | ||
Deployments NamespaceKind = "deployments" | ||
ReplicaSets NamespaceKind = "replicasets" | ||
ReplicationControllers NamespaceKind = "replicationcontrollers" | ||
StatefulSets NamespaceKind = "statefulsets" | ||
DaemonSets NamespaceKind = "daemonsets" | ||
CronJobs NamespaceKind = "cronjobs" | ||
Services NamespaceKind = "services" | ||
Jobs NamespaceKind = "jobs" | ||
Pods NamespaceKind = "pods" | ||
ConfigMaps NamespaceKind = "configmaps" | ||
Roles NamespaceKind = "roles" | ||
RoleBindings NamespaceKind = "rolebindings" | ||
NetworkPolicys NamespaceKind = "networkpolicies" | ||
Ingresss NamespaceKind = "ingresses" | ||
ResourceQuotas NamespaceKind = "resourcequotas" | ||
LimitRanges NamespaceKind = "limitranges" | ||
) | ||
|
||
const ( | ||
ComponentStatus ClusterKind = "componentstatuses" | ||
Nodes ClusterKind = "nodes" | ||
Namespaces ClusterKind = "namespaces" | ||
PersistentVolumes ClusterKind = "persistentvolumes" | ||
ClusterRoles ClusterKind = "clusterroles" | ||
ClusterRoleBindings ClusterKind = "clusterrolebindings" | ||
PodSecurityPolicies ClusterKind = "podsecuritypolicies" | ||
) | ||
|
||
func (k NamespaceKind) String() string { | ||
return string(k) | ||
} | ||
|
||
func (k ClusterKind) String() string { | ||
return string(k) | ||
} | ||
|
||
func GetNamespaceKinds() []NamespaceKind { | ||
return []NamespaceKind{ | ||
Deployments, | ||
ReplicaSets, | ||
ReplicationControllers, | ||
StatefulSets, | ||
DaemonSets, | ||
CronJobs, | ||
Services, | ||
Jobs, | ||
Pods, | ||
ConfigMaps, | ||
Roles, | ||
RoleBindings, | ||
NetworkPolicys, | ||
Ingresss, | ||
ResourceQuotas, | ||
LimitRanges, | ||
} | ||
} | ||
|
||
func GetClusterKinds() []ClusterKind { | ||
return []ClusterKind{ | ||
ComponentStatus, | ||
Nodes, | ||
Namespaces, | ||
PersistentVolumes, | ||
ClusterRoles, | ||
ClusterRoleBindings, | ||
PodSecurityPolicies, | ||
} | ||
} | ||
|
||
func IsNamespaceKind(kind string) bool { | ||
for _, namespaceKind := range GetNamespaceKinds() { | ||
if namespaceKind.String() == kind { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
func IsClusterKind(kind string) bool { | ||
for _, clusterKind := range GetClusterKinds() { | ||
if clusterKind.String() == kind { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
Oops, something went wrong.