|
| 1 | +package create |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/BeamStackProj/beamstack-cli/src/objects" |
| 8 | + "github.com/BeamStackProj/beamstack-cli/src/types" |
| 9 | + "github.com/BeamStackProj/beamstack-cli/src/utils" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + |
| 12 | + v1 "k8s.io/api/core/v1" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/client-go/kubernetes" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + cpu string = "2" |
| 19 | + memory string = "2048Mi" |
| 20 | + cpuLimit string = "2" |
| 21 | + memoryLimit string = "2048Mi" |
| 22 | + volumeSize string = "1Gi" |
| 23 | + taskslots uint8 = 10 |
| 24 | + replicas uint8 = 1 |
| 25 | +) |
| 26 | + |
| 27 | +// infoCmd represents the info command |
| 28 | +var FlinkClusterCmd = &cobra.Command{ |
| 29 | + Use: "flink-cluster", |
| 30 | + Short: "create a flink cluster", |
| 31 | + Long: `create a flink cluster`, |
| 32 | + Args: cobra.ExactArgs(1), |
| 33 | + Run: func(cmd *cobra.Command, args []string) { |
| 34 | + |
| 35 | + profile, err := utils.ValidateCluster() |
| 36 | + |
| 37 | + if err != nil { |
| 38 | + fmt.Println(err) |
| 39 | + return |
| 40 | + } |
| 41 | + if profile.Operators.Flink == nil { |
| 42 | + fmt.Println("Flink Operator not initialized on this cluster") |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + namespace := "flink" |
| 47 | + flinkVersion := strings.Replace(profile.Operators.Flink.Version, ".", "_", 1) |
| 48 | + flinkImage := fmt.Sprintf("beamstackproj/flink-v%s:latest", flinkVersion) |
| 49 | + taskmanagerImage := fmt.Sprintf("beamstackproj/flink-harness%s:latest", flinkVersion) |
| 50 | + ClaimName := fmt.Sprintf("%s-pvc", args[0]) |
| 51 | + fmt.Printf("creating flink cluster %s\n", args[0]) |
| 52 | + |
| 53 | + config := utils.GetKubeConfig() |
| 54 | + |
| 55 | + clientset, err := kubernetes.NewForConfig(config) |
| 56 | + if err != nil { |
| 57 | + fmt.Println(err) |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + if err := objects.CreatePVC(clientset, ClaimName, namespace, volumeSize); err != nil { |
| 62 | + fmt.Println(err) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + spec := types.FlinkDeploymentSpec{ |
| 67 | + Image: &flinkImage, |
| 68 | + ImagePullPolicy: "IfNotPresent", |
| 69 | + FlinkVersion: flinkVersion, |
| 70 | + FlinkConfiguration: map[string]string{ |
| 71 | + "taskmanager.numberOfTaskSlots": string(taskslots), |
| 72 | + }, |
| 73 | + ServiceAccountName: "flink", |
| 74 | + PodTemplate: &v1.PodTemplateSpec{ |
| 75 | + Spec: v1.PodSpec{ |
| 76 | + Containers: []v1.Container{ |
| 77 | + { |
| 78 | + Name: "flink-main-container", |
| 79 | + VolumeMounts: []v1.VolumeMount{ |
| 80 | + { |
| 81 | + MountPath: "/opt/flink/log", |
| 82 | + Name: "flink-logs", |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + }, |
| 87 | + Volumes: []v1.Volume{ |
| 88 | + { |
| 89 | + Name: "flink-logs", |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + JobManager: types.JobManagerSpec{ |
| 95 | + Resource: types.Resource{ |
| 96 | + Memory: memory, |
| 97 | + CPU: cpu, |
| 98 | + }, |
| 99 | + }, |
| 100 | + TaskManager: types.TaskManagerSpec{ |
| 101 | + Replicas: replicas, |
| 102 | + Resource: types.Resource{ |
| 103 | + Memory: memory, |
| 104 | + CPU: cpu, |
| 105 | + }, |
| 106 | + |
| 107 | + PodTemplate: &v1.PodTemplateSpec{ |
| 108 | + Spec: v1.PodSpec{ |
| 109 | + Containers: []v1.Container{ |
| 110 | + { |
| 111 | + Name: "worker", |
| 112 | + Image: taskmanagerImage, |
| 113 | + Args: []string{"-worker_pool"}, |
| 114 | + Ports: []v1.ContainerPort{ |
| 115 | + { |
| 116 | + Name: "harness-port", |
| 117 | + ContainerPort: 50000, |
| 118 | + }, |
| 119 | + }, |
| 120 | + VolumeMounts: []v1.VolumeMount{ |
| 121 | + { |
| 122 | + MountPath: "/pvc", |
| 123 | + Name: "flink-cluster-pvc", |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + }, |
| 128 | + Volumes: []v1.Volume{ |
| 129 | + { |
| 130 | + Name: "flink-cluster-pvc", |
| 131 | + VolumeSource: v1.VolumeSource{ |
| 132 | + PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ |
| 133 | + ClaimName: ClaimName, |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + } |
| 142 | + |
| 143 | + err = objects.CreateDynamicResource( |
| 144 | + metav1.TypeMeta{ |
| 145 | + APIVersion: "flink.apache.org/v1beta1", |
| 146 | + Kind: "FlinkDeployment", |
| 147 | + }, |
| 148 | + metav1.ObjectMeta{ |
| 149 | + Name: args[0], |
| 150 | + Namespace: "flink", |
| 151 | + }, |
| 152 | + spec, |
| 153 | + ) |
| 154 | + |
| 155 | + if err != nil { |
| 156 | + fmt.Println(err) |
| 157 | + return |
| 158 | + } |
| 159 | + }, |
| 160 | +} |
| 161 | + |
| 162 | +func init() { |
| 163 | + FlinkClusterCmd.Flags().StringVar(&cpu, "cpu", cpu, "Cpu request for task manager") |
| 164 | + FlinkClusterCmd.Flags().StringVar(&cpuLimit, "cpuLimit", cpu, "Cpu request for task manager") |
| 165 | + FlinkClusterCmd.Flags().StringVar(&memory, "memory", memory, "Cpu request for task manager") |
| 166 | + FlinkClusterCmd.Flags().StringVar(&memoryLimit, "memoryLimit", memoryLimit, "Cpu request for task manager") |
| 167 | + FlinkClusterCmd.Flags().Uint8Var(&replicas, "replicas", replicas, "numbers of replicas sets for task manager") |
| 168 | + FlinkClusterCmd.Flags().Uint8Var(&taskslots, "taskslots", taskslots, "numbers of taskslots to be created for the task manager") |
| 169 | + FlinkClusterCmd.Flags().StringVar(&volumeSize, "volumeSize", volumeSize, "size of persistent volume to be attached to flink cluster") |
| 170 | +} |
0 commit comments