Skip to content

Commit

Permalink
Merge pull request kosmos-io#73 from ONE7live/main
Browse files Browse the repository at this point in the history
feat: Kosmosctl supports knode-manager module install and uninstall
  • Loading branch information
kosmos-robot authored Oct 7, 2023
2 parents a83c62d + 7c30c83 commit 1bc0551
Show file tree
Hide file tree
Showing 9 changed files with 406 additions and 75 deletions.
149 changes: 140 additions & 9 deletions pkg/kosmosctl/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package install
import (
"context"
"fmt"
"os"

"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
Expand All @@ -20,9 +21,11 @@ import (
)

type CommandInstallOptions struct {
Namespace string
ImageRegistry string
Version string
Namespace string
ImageRegistry string
Version string
Module string
HostKubeConfig string

Client kubernetes.Interface
ExtensionsClient extensionsclient.Interface
Expand Down Expand Up @@ -50,6 +53,8 @@ func NewCmdInstall(f ctlutil.Factory) *cobra.Command {
flags := cmd.Flags()
flags.StringVarP(&o.Namespace, "namespace", "n", util.DefaultNamespace, "Kosmos namespace.")
flags.StringVarP(&o.ImageRegistry, "private-image-registry", "", util.DefaultImageRepository, "Private image registry where pull images from. If set, all required images will be downloaded from it, it would be useful in offline installation scenarios. In addition, you still can use --kube-image-registry to specify the registry for Kubernetes's images.")
flags.StringVarP(&o.Module, "module", "m", util.DefaultInstallModule, "Kosmos specify the module to install.")
flags.StringVar(&o.HostKubeConfig, "host-kubeconfig", "", "Absolute path to the host kubeconfig file.")

return cmd
}
Expand Down Expand Up @@ -78,16 +83,46 @@ func (o *CommandInstallOptions) Validate() error {
return fmt.Errorf("namespace must be specified")
}

if o.Module != "clusterlink" && o.HostKubeConfig == "" {
return fmt.Errorf("host-kubeconfig must be specified")
}

return nil
}

func (o *CommandInstallOptions) Run() error {
switch o.Module {
case "clusterlink":
err := o.runClusterlink()
if err != nil {
return err
}
case "clusterrouter":
err := o.runClusterrouter()
if err != nil {
return err
}
case "all":
err := o.runClusterlink()
if err != nil {
return err
}
err = o.runClusterrouter()
if err != nil {
return err
}
}

return nil
}

func (o *CommandInstallOptions) runClusterlink() error {
namespace := &corev1.Namespace{}
namespace.Name = o.Namespace
_, err := o.Client.CoreV1().Namespaces().Create(context.TODO(), namespace, metav1.CreateOptions{})
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kosmosctl install run error, namespace options failed: %v", err)
return fmt.Errorf("kosmosctl install clusterlink run error, namespace options failed: %v", err)
}
}

Expand All @@ -100,7 +135,7 @@ func (o *CommandInstallOptions) Run() error {
_, err = o.Client.CoreV1().ServiceAccounts(o.Namespace).Create(context.TODO(), clusterlinkServiceAccount, metav1.CreateOptions{})
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kosmosctl install run error, serviceaccount options failed: %v", err)
return fmt.Errorf("kosmosctl install clusterlink run error, serviceaccount options failed: %v", err)
}
}

Expand All @@ -111,7 +146,7 @@ func (o *CommandInstallOptions) Run() error {
_, err = o.Client.RbacV1().ClusterRoles().Create(context.TODO(), clusterlinkClusterRole, metav1.CreateOptions{})
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kosmosctl install run error, clusterrole options failed: %v", err)
return fmt.Errorf("kosmosctl install clusterlink run error, clusterrole options failed: %v", err)
}
}

Expand All @@ -124,7 +159,7 @@ func (o *CommandInstallOptions) Run() error {
_, err = o.Client.RbacV1().ClusterRoleBindings().Create(context.TODO(), clusterlinkClusterRoleBinding, metav1.CreateOptions{})
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kosmosctl install run error, clusterrolebinding options failed: %v", err)
return fmt.Errorf("kosmosctl install clusterlink run error, clusterrolebinding options failed: %v", err)
}
}

Expand All @@ -147,7 +182,7 @@ func (o *CommandInstallOptions) Run() error {
_, err = o.ExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), &crds.Items[i], metav1.CreateOptions{})
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kosmosctl install run error, crd options failed: %v", err)
return fmt.Errorf("kosmosctl install clusterlink run error, crd options failed: %v", err)
}
}
}
Expand All @@ -163,7 +198,103 @@ func (o *CommandInstallOptions) Run() error {
_, err = o.Client.AppsV1().Deployments(o.Namespace).Create(context.Background(), deployment, metav1.CreateOptions{})
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kosmosctl install run error, deployment options failed: %v", err)
return fmt.Errorf("kosmosctl install clusterlink run error, deployment options failed: %v", err)
}
}

return nil
}

func (o *CommandInstallOptions) runClusterrouter() error {
namespace := &corev1.Namespace{}
namespace.Name = o.Namespace
_, err := o.Client.CoreV1().Namespaces().Create(context.TODO(), namespace, metav1.CreateOptions{})
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kosmosctl install clusterrouter run error, namespace options failed: %v", err)
}
}

hostKubeconfig, err := os.ReadFile(o.HostKubeConfig)
if err != nil {
return fmt.Errorf("kosmosctl install clusterrouter run error, host-kubeconfig read failed: %v", err)
}
clusterRouterConfigMap := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: "host-kubeconfig",
Namespace: o.Namespace,
},
Data: map[string]string{
"kubeconfig": string(hostKubeconfig),
},
}
_, err = o.Client.CoreV1().ConfigMaps(o.Namespace).Create(context.TODO(), clusterRouterConfigMap, metav1.CreateOptions{})
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kosmosctl install clusterrouter run error, configmap options failed: %v", err)
}
}

clusterRouterServiceAccount, err := util.GenerateServiceAccount(manifest.ClusterRouterKnodeServiceAccount, manifest.ServiceAccountReplace{
Namespace: o.Namespace,
})
if err != nil {
return err
}
_, err = o.Client.CoreV1().ServiceAccounts(o.Namespace).Create(context.TODO(), clusterRouterServiceAccount, metav1.CreateOptions{})
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kosmosctl install clusterrouter run error, serviceaccount options failed: %v", err)
}
}

clusterRouterClusterRole, err := util.GenerateClusterRole(manifest.ClusterRouterKnodeClusterRole, nil)
if err != nil {
return err
}
_, err = o.Client.RbacV1().ClusterRoles().Create(context.TODO(), clusterRouterClusterRole, metav1.CreateOptions{})
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kosmosctl install clusterrouter run error, clusterrole options failed: %v", err)
}
}

clusterRouterClusterRoleBinding, err := util.GenerateClusterRoleBinding(manifest.ClusterRouterKnodeClusterRoleBinding, manifest.ClusterRoleBindingReplace{
Namespace: o.Namespace,
})
if err != nil {
return err
}
_, err = o.Client.RbacV1().ClusterRoleBindings().Create(context.TODO(), clusterRouterClusterRoleBinding, metav1.CreateOptions{})
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kosmosctl install clusterrouter run error, clusterrolebinding options failed: %v", err)
}
}

clusterRouterKnode, err := util.GenerateCustomResourceDefinition(manifest.ClusterRouterKnode, nil)
if err != nil {
return err
}
_, err = o.ExtensionsClient.ApiextensionsV1().CustomResourceDefinitions().Create(context.Background(), clusterRouterKnode, metav1.CreateOptions{})
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kosmosctl install clusterrouter run error, crd options failed: %v", err)
}
}

deployment, err := util.GenerateDeployment(manifest.ClusterRouterKnodeDeployment, manifest.DeploymentReplace{
Namespace: o.Namespace,
ImageRepository: o.ImageRegistry,
Version: version.GetReleaseVersion().PatchRelease(),
})
if err != nil {
return err
}
_, err = o.Client.AppsV1().Deployments(o.Namespace).Create(context.Background(), deployment, metav1.CreateOptions{})
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return fmt.Errorf("kosmosctl install clusterrouter run error, deployment options failed: %v", err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/kosmosctl/kosmosctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewKosmosCtlCommand() *cobra.Command {
Message: "Install/UnInstall Commands:",
Commands: []*cobra.Command{
install.NewCmdInstall(f),
uninstall.NewCmdUninstall(f, ioStreams),
uninstall.NewCmdUninstall(f),
},
},
{
Expand Down
16 changes: 16 additions & 0 deletions pkg/kosmosctl/manifest/manifest_clusterrolebindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ subjects:
name: clusterlink-operator
namespace: clusterlink-system
`

ClusterRouterKnodeClusterRoleBinding = `
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: clusterrouter-knode
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: clusterrouter-knode
subjects:
- kind: ServiceAccount
name: clusterrouter-knode
namespace: {{ .Namespace }}
`
)

type ClusterRoleBindingReplace struct {
Expand Down
14 changes: 14 additions & 0 deletions pkg/kosmosctl/manifest/manifest_clusterroles.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,18 @@ rules:
- nonResourceURLs: ['*']
verbs: ["get"]
`

ClusterRouterKnodeClusterRole = `
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: clusterrouter-knode
rules:
- apiGroups: ['*']
resources: ['*']
verbs: ["*"]
- nonResourceURLs: ['*']
verbs: ["get"]
`
)
120 changes: 120 additions & 0 deletions pkg/kosmosctl/manifest/manifest_crds.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,4 +352,124 @@ spec:
subresources:
status: {}
`

ClusterRouterKnode = `---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.11.0
creationTimestamp: null
name: knodes.kosmos.io
spec:
group: kosmos.io
names:
kind: Knode
listKind: KnodeList
plural: knodes
singular: knode
scope: Cluster
versions:
- name: v1alpha1
schema:
openAPIV3Schema:
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
properties:
disableTaint:
type: boolean
kubeconfig:
format: byte
type: string
nodeName:
type: string
type:
default: k8s
type: string
type: object
status:
properties:
apiserver:
type: string
conditions:
items:
description: "Condition contains details for one aspect of the current
state of this API Resource. --- This struct is intended for direct
use as an array at the field path .status.conditions."
properties:
lastTransitionTime:
description: lastTransitionTime is the last time the condition
transitioned from one status to another. This should be when
the underlying condition changed. If that is not known, then
using the time when the API field changed is acceptable.
format: date-time
type: string
message:
description: message is a human readable message indicating
details about the transition. This may be an empty string.
maxLength: 32768
type: string
observedGeneration:
description: observedGeneration represents the .metadata.generation
that the condition was set based upon. For instance, if .metadata.generation
is currently 12, but the .status.conditions[x].observedGeneration
is 9, the condition is out of date with respect to the current
state of the instance.
format: int64
minimum: 0
type: integer
reason:
description: reason contains a programmatic identifier indicating
the reason for the condition's last transition. Producers
of specific condition types may define expected values and
meanings for this field, and whether the values are considered
a guaranteed API. The value should be a CamelCase string.
This field may not be empty.
maxLength: 1024
minLength: 1
pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$
type: string
status:
description: status of the condition, one of True, False, Unknown.
enum:
- "True"
- "False"
- Unknown
type: string
type:
description: type of condition in CamelCase or in foo.example.com/CamelCase.
--- Many .condition.type values are consistent across resources
like Available, but because arbitrary conditions can be useful
(see .node.status.conditions), the ability to deconflict is
important. The regex it matches is (dns1123SubdomainFmt/)?(qualifiedNameFmt)
maxLength: 316
pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$
type: string
required:
- lastTransitionTime
- message
- reason
- status
- type
type: object
type: array
version:
type: string
type: object
type: object
served: true
storage: true
`
)
Loading

0 comments on commit 1bc0551

Please sign in to comment.