Skip to content

Commit 16ebec6

Browse files
committed
cli: Infer gloo deploy name
1 parent 443933d commit 16ebec6

File tree

6 files changed

+72
-6
lines changed

6 files changed

+72
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
changelog:
2+
- type: FIX
3+
issueLink: https://github.com/solo-io/gloo/issues/9163
4+
resolvesIssue: false
5+
description: Infer the gloo deployment name in cases where the deployment name is not the default `gloo`. The gloo deployment is identified by the `gloo=gloo` label.

projects/gloo/cli/pkg/cmd/check/gloo_stats.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88

99
"github.com/solo-io/gloo/pkg/cliutil"
1010
"github.com/solo-io/gloo/projects/gloo/cli/pkg/cmd/options"
11+
"github.com/solo-io/gloo/projects/gloo/cli/pkg/helpers"
1112
"github.com/solo-io/gloo/projects/gloo/pkg/defaults"
1213
v1 "k8s.io/api/apps/v1"
1314
)
1415

1516
const (
16-
glooDeployment = "gloo"
1717
rateLimitDeployment = "rate-limit"
1818
glooStatsPath = "/metrics"
1919

@@ -28,6 +28,9 @@ var (
2828
return fmt.Sprintf("Gloo has detected that the data plane is out of sync. The following types of resources have not been accepted: %v. "+
2929
"Gloo will not be able to process any other configuration updates until these errors are resolved.", resourceNames)
3030
}
31+
32+
// Initialize the custom deployment name that is overwritten later on
33+
customGlooDeploymentName = helpers.GlooDeploymentName
3134
)
3235

3336
func ResourcesSyncedOverXds(stats, deploymentName string) bool {
@@ -83,7 +86,7 @@ func checkXdsMetrics(ctx context.Context, opts *options.Options, deployments *v1
8386
printer.AppendCheck("Warning: checking xds with port forwarding is disabled\n")
8487
return nil
8588
}
86-
stats, portFwdCmd, err := cliutil.PortForwardGet(ctx, opts.Metadata.GetNamespace(), "deploy/"+glooDeployment,
89+
stats, portFwdCmd, err := cliutil.PortForwardGet(ctx, opts.Metadata.GetNamespace(), "deploy/"+customGlooDeploymentName,
8790
localPort, adminPort, false, glooStatsPath)
8891
if err != nil {
8992
return err
@@ -94,12 +97,12 @@ func checkXdsMetrics(ctx context.Context, opts *options.Options, deployments *v1
9497
}
9598

9699
if strings.TrimSpace(stats) == "" {
97-
err := fmt.Sprint(errMessage+": could not find any metrics at", glooStatsPath, "endpoint of the "+glooDeployment+" deployment")
100+
err := fmt.Sprint(errMessage+": could not find any metrics at", glooStatsPath, "endpoint of the "+customGlooDeploymentName+" deployment")
98101
fmt.Println(err)
99102
return fmt.Errorf(err)
100103
}
101104

102-
if !ResourcesSyncedOverXds(stats, glooDeployment) {
105+
if !ResourcesSyncedOverXds(stats, customGlooDeploymentName) {
103106
fmt.Println(errMessage)
104107
return fmt.Errorf(errMessage)
105108
}

projects/gloo/cli/pkg/cmd/check/root.go

+5
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ func CheckResources(opts *options.Options) error {
115115
multiErr = multierror.Append(multiErr, err)
116116
}
117117
}
118+
// Fetch the gloo deployment name even if check deployments is disabled as it is used in other checks
119+
customGlooDeploymentName, err = helpers.GetGlooDeploymentName(opts.Top.Ctx, opts.Metadata.GetNamespace())
120+
if err != nil {
121+
multiErr = multierror.Append(multiErr, err)
122+
}
118123

119124
if included := doesNotContain(opts.Top.CheckName, "pods"); included {
120125
err := checkPods(ctx, opts)

projects/gloo/cli/pkg/common/get.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ func getProxiesFromK8s(name string, opts *options.Options) (gloov1.ProxyList, er
186186
// if name is empty, return all proxies
187187
func getProxiesFromGrpc(name string, namespace string, opts *options.Options, proxyEndpointPort string) (gloov1.ProxyList, error) {
188188

189+
glooDeploymentName, err := helpers.GetGlooDeploymentName(opts.Top.Ctx, opts.Metadata.GetNamespace())
190+
if err != nil {
191+
return nil, err
192+
}
193+
189194
options := []grpc.CallOption{
190195
// Some proxies can become very large and exceed the default 100Mb limit
191196
// For this reason we want remove the limit but will settle for a limit of MaxInt32
@@ -198,7 +203,7 @@ func getProxiesFromGrpc(name string, namespace string, opts *options.Options, pr
198203
return nil, err
199204
}
200205
localPort := strconv.Itoa(freePort)
201-
portFwdCmd, err := cliutil.PortForward(opts.Metadata.GetNamespace(), "deployment/gloo",
206+
portFwdCmd, err := cliutil.PortForward(opts.Metadata.GetNamespace(), "deployment/"+glooDeploymentName,
202207
localPort, proxyEndpointPort, opts.Top.Verbose)
203208
if portFwdCmd.Process != nil {
204209
defer portFwdCmd.Process.Release()

projects/gloo/cli/pkg/helpers/clients.go

+42
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ var (
4747
lock sync.Mutex
4848
)
4949

50+
const (
51+
GlooDeploymentName = "gloo"
52+
)
53+
5054
// iterates over all the factory overrides, returning the first non-nil
5155
// mem > consul
5256
// if none set, return nil (callers will default to Kube CRD)
@@ -149,6 +153,44 @@ func KubeClientWithKubecontext(kubecontext string) (kubernetes.Interface, error)
149153
return clientset, nil
150154
}
151155

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+
152194
func MustGetNamespaces(ctx context.Context) []string {
153195
ns, err := GetNamespaces(ctx)
154196
if err != nil {

projects/gloo/cli/pkg/xdsinspection/get_last_config.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
structpb "github.com/golang/protobuf/ptypes/struct"
2626
"github.com/rotisserie/eris"
2727
_ "github.com/solo-io/gloo/projects/envoyinit/hack/filter_types"
28+
"github.com/solo-io/gloo/projects/gloo/cli/pkg/helpers"
2829
"github.com/solo-io/gloo/projects/gloo/pkg/defaults"
2930
"github.com/solo-io/go-utils/contextutils"
3031
"go.uber.org/zap"
@@ -40,14 +41,19 @@ const (
4041
// According to gloo
4142
func GetGlooXdsDump(ctx context.Context, proxyName, namespace string, verboseErrors bool) (*XdsDump, error) {
4243

44+
glooDeploymentName, err := helpers.GetGlooDeploymentName(ctx, namespace)
45+
if err != nil {
46+
return nil, err
47+
}
48+
4349
xdsPort := strconv.Itoa(int(defaults.GlooXdsPort))
4450
// If gloo is in MTLS mode
4551
glooMtlsCheck := exec.Command("kubectl", "get", "configmap", envoySidecarConfig, "-n", namespace)
4652
if err := glooMtlsCheck.Run(); err == nil {
4753
xdsPort = strconv.Itoa(int(defaults.GlooMtlsModeXdsPort))
4854
}
4955
portFwd := exec.Command("kubectl", "port-forward", "-n", namespace,
50-
"deployment/gloo", xdsPort)
56+
"deployment/"+glooDeploymentName, xdsPort)
5157
mergedPortForwardOutput := bytes.NewBuffer([]byte{})
5258
portFwd.Stdout = mergedPortForwardOutput
5359
portFwd.Stderr = mergedPortForwardOutput

0 commit comments

Comments
 (0)