-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
167 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
Copyright 2024. | ||
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.0 | ||
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 controller | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"flag" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"regexp" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
// appsv1 "k8s.io/api/apps/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/clientcmd" | ||
"k8s.io/client-go/util/homedir" | ||
) | ||
|
||
func find_service_account(file_name string) string { | ||
file, err := os.Open(file_name) | ||
if err != nil { | ||
fmt.Println("Error opening file:", err) | ||
return "" | ||
} | ||
defer file.Close() | ||
|
||
// Create a regular expression to match the service account | ||
re := regexp.MustCompile(`--service-account\s+(\S+)`) | ||
|
||
// Create a scanner to read the file line by line | ||
scanner := bufio.NewScanner(file) | ||
|
||
// Iterate through each line of the file | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
// Check if the line contains the service account | ||
match := re.FindStringSubmatch(line) | ||
if len(match) > 1 { | ||
// Extract the service account from the matched line | ||
serviceAccount := match[1] | ||
return serviceAccount | ||
} | ||
} | ||
|
||
// Check for scanner errors | ||
if err := scanner.Err(); err != nil { | ||
return "" | ||
} | ||
|
||
return "" | ||
} | ||
|
||
func attach_volume_to_waypoint(service_name, service_account string) { | ||
// Set up kubeconfig path | ||
var k8sconfig *string | ||
if home := homedir.HomeDir(); home != "" { | ||
k8sconfig = flag.String("k8sconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") | ||
} else { | ||
k8sconfig = flag.String("k8sconfig", "", "absolute path to the k8sconfig file") | ||
} | ||
flag.Parse() | ||
|
||
// Use the current context in kubeconfig | ||
config, err := clientcmd.BuildConfigFromFlags("", *k8sconfig) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
// Create the clientset | ||
clientset, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
// Set deployment details | ||
namespace := "default" | ||
deploymentName := service_account + "-istio-waypoint" | ||
pvcName := service_name + "-pvc" | ||
mountPath := "/data" | ||
|
||
// Get the specified deployment | ||
deployment, err := clientset.AppsV1().Deployments(namespace).Get(context.TODO(), deploymentName, metav1.GetOptions{}) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
// Define the volume and volume mount | ||
volume := corev1.Volume{ | ||
Name: service_name + "-storage", | ||
VolumeSource: corev1.VolumeSource{ | ||
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{ | ||
ClaimName: pvcName, | ||
}, | ||
}, | ||
} | ||
volumeMount := corev1.VolumeMount{ | ||
Name: service_name + "-storage", | ||
MountPath: mountPath, | ||
} | ||
|
||
// Attach the volume to the deployment spec | ||
deployment.Spec.Template.Spec.Volumes = append(deployment.Spec.Template.Spec.Volumes, volume) | ||
for i := range deployment.Spec.Template.Spec.Containers { | ||
deployment.Spec.Template.Spec.Containers[i].VolumeMounts = append(deployment.Spec.Template.Spec.Containers[i].VolumeMounts, volumeMount) | ||
} | ||
|
||
// Update the deployment | ||
_, err = clientset.AppsV1().Deployments(namespace).Update(context.TODO(), deployment, metav1.UpdateOptions{}) | ||
if err != nil { | ||
panic(err.Error()) | ||
} | ||
|
||
// fmt.Printf("Deployment '%s' in namespace '%s' updated successfully.\n", deploymentName, namespace) | ||
} |
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