|
47 | 47 | lock sync.Mutex
|
48 | 48 | )
|
49 | 49 |
|
| 50 | +const ( |
| 51 | + GlooDeploymentName = "gloo" |
| 52 | +) |
| 53 | + |
50 | 54 | // iterates over all the factory overrides, returning the first non-nil
|
51 | 55 | // mem > consul
|
52 | 56 | // if none set, return nil (callers will default to Kube CRD)
|
@@ -149,6 +153,44 @@ func KubeClientWithKubecontext(kubecontext string) (kubernetes.Interface, error)
|
149 | 153 | return clientset, nil
|
150 | 154 | }
|
151 | 155 |
|
| 156 | +func GetGlooDeploymentName(ctx context.Context, namespace string) (string, error) { |
| 157 | + client, err := KubeClient() |
| 158 | + if err != nil { |
| 159 | + errMessage := "error getting KubeClient" |
| 160 | + fmt.Println(errMessage) |
| 161 | + return "", fmt.Errorf(errMessage+": %v", err) |
| 162 | + } |
| 163 | + _, err = client.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{}) |
| 164 | + if err != nil { |
| 165 | + errMessage := "Gloo namespace does not exist" |
| 166 | + fmt.Println(errMessage) |
| 167 | + return "", fmt.Errorf(errMessage+": %v", err) |
| 168 | + } |
| 169 | + deployments, err := client.AppsV1().Deployments(namespace).List(ctx, metav1.ListOptions{ |
| 170 | + LabelSelector: "gloo=gloo", |
| 171 | + }) |
| 172 | + if err != nil { |
| 173 | + return "", err |
| 174 | + } |
| 175 | + if len(deployments.Items) == 1 { |
| 176 | + return deployments.Items[0].Name, nil |
| 177 | + } |
| 178 | + errMessage := "Unable to find the gloo deployment" |
| 179 | + // if there are multiple we can reasonably use the default variant |
| 180 | + for _, d := range deployments.Items { |
| 181 | + if d.Name != GlooDeploymentName { |
| 182 | + // At least 1 deployment exists, in case we dont find default update our error message |
| 183 | + errMessage = "too many app=gloo deployments, cannot decide which to target" |
| 184 | + continue |
| 185 | + } |
| 186 | + // TODO: (nfuden) Remove this, while we should generally avoid println in our formatted output we already have alot of these |
| 187 | + fmt.Println("multiple gloo labeled apps found, defaulting to", GlooDeploymentName) |
| 188 | + return GlooDeploymentName, nil |
| 189 | + } |
| 190 | + fmt.Println(errMessage) |
| 191 | + return "", fmt.Errorf(errMessage+": %v", err) |
| 192 | +} |
| 193 | + |
152 | 194 | func MustGetNamespaces(ctx context.Context) []string {
|
153 | 195 | ns, err := GetNamespaces(ctx)
|
154 | 196 | if err != nil {
|
|
0 commit comments