Skip to content

Commit

Permalink
[1.13] cli: Infer gloo deploy name (#9719)
Browse files Browse the repository at this point in the history
* cli: Infer gloo deployment name

* adding changelog

* fix tests

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

Co-authored-by: Nathan Fudenberg <[email protected]>

* fix syntax

---------

Co-authored-by: Nathan Fudenberg <[email protected]>
  • Loading branch information
davidjumani and nfuden authored Jul 4, 2024
1 parent 427c79a commit feb451a
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 6 deletions.
5 changes: 5 additions & 0 deletions changelog/v1.13.39/infer-gloo-deploy-name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
changelog:
- type: FIX
issueLink: https://github.com/solo-io/gloo/issues/9163
resolvesIssue: false
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.
11 changes: 7 additions & 4 deletions projects/gloo/cli/pkg/cmd/check/gloo_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (

"github.com/solo-io/gloo/pkg/cliutil"
"github.com/solo-io/gloo/projects/gloo/cli/pkg/cmd/options"
"github.com/solo-io/gloo/projects/gloo/cli/pkg/helpers"
"github.com/solo-io/gloo/projects/gloo/pkg/defaults"
v1 "k8s.io/api/apps/v1"
)

const (
glooDeployment = "gloo"
rateLimitDeployment = "rate-limit"
glooStatsPath = "/metrics"

Expand All @@ -27,6 +27,9 @@ var (
return fmt.Sprintf("Gloo has detected that the data plane is out of sync. The following types of resources have not been accepted: %v. "+
"Gloo will not be able to process any other configuration updates until these errors are resolved.", resourceNames)
}

// Initialize the custom deployment name that is overwritten later on
customGlooDeploymentName = helpers.GlooDeploymentName
)

func ResourcesSyncedOverXds(stats, deploymentName string) bool {
Expand Down Expand Up @@ -78,7 +81,7 @@ func checkXdsMetrics(opts *options.Options, glooNamespace string, deployments *v
localPort := strconv.Itoa(freePort)
adminPort := strconv.Itoa(int(defaults.GlooAdminPort))
// stats is the string containing all stats from /stats/prometheus
stats, portFwdCmd, err := cliutil.PortForwardGet(opts.Top.Ctx, glooNamespace, "deploy/"+glooDeployment,
stats, portFwdCmd, err := cliutil.PortForwardGet(opts.Top.Ctx, glooNamespace, "deploy/"+customGlooDeploymentName,
localPort, adminPort, false, glooStatsPath)
if err != nil {
return err
Expand All @@ -89,12 +92,12 @@ func checkXdsMetrics(opts *options.Options, glooNamespace string, deployments *v
}

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

if !ResourcesSyncedOverXds(stats, glooDeployment) {
if !ResourcesSyncedOverXds(stats, customGlooDeploymentName) {
fmt.Println(errMessage)
return fmt.Errorf(errMessage)
}
Expand Down
5 changes: 5 additions & 0 deletions projects/gloo/cli/pkg/cmd/check/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ func CheckResources(opts *options.Options) error {
multiErr = multierror.Append(multiErr, err)
}
}
// Fetch the gloo deployment name even if check deployments is disabled as it is used in other checks
customGlooDeploymentName, err = helpers.GetGlooDeploymentName(opts.Top.Ctx, opts.Metadata.GetNamespace())
if err != nil {
multiErr = multierror.Append(multiErr, err)
}

if included := doesNotContain(opts.Top.CheckName, "pods"); included {
err := checkPods(ctx, opts)
Expand Down
12 changes: 12 additions & 0 deletions projects/gloo/cli/pkg/cmd/check/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ var _ = Describe("Root", func() {
ObjectMeta: metav1.ObjectMeta{
Name: appName,
Namespace: "gloo-system",
Labels: map[string]string{
"gloo": "gloo",
},
},
Spec: appsv1.DeploymentSpec{},
}, metav1.CreateOptions{})
Expand Down Expand Up @@ -95,6 +98,9 @@ var _ = Describe("Root", func() {
ObjectMeta: metav1.ObjectMeta{
Name: appName,
Namespace: "gloo-system",
Labels: map[string]string{
"gloo": "gloo",
},
},
Spec: appsv1.DeploymentSpec{},
}, metav1.CreateOptions{})
Expand Down Expand Up @@ -176,6 +182,9 @@ var _ = Describe("Root", func() {
ObjectMeta: metav1.ObjectMeta{
Name: appName,
Namespace: myNs,
Labels: map[string]string{
"gloo": "gloo",
},
},
Spec: appsv1.DeploymentSpec{},
}, metav1.CreateOptions{})
Expand Down Expand Up @@ -221,6 +230,9 @@ var _ = Describe("Root", func() {
ObjectMeta: metav1.ObjectMeta{
Name: appName,
Namespace: "gloo-system",
Labels: map[string]string{
"gloo": "gloo",
},
},
Spec: appsv1.DeploymentSpec{},
}, metav1.CreateOptions{})
Expand Down
7 changes: 6 additions & 1 deletion projects/gloo/cli/pkg/common/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ func getProxiesFromK8s(name string, opts *options.Options) (gloov1.ProxyList, er
// if name is empty, return all proxies
func getProxiesFromGrpc(name string, namespace string, opts *options.Options, proxyEndpointPort string) (gloov1.ProxyList, error) {

glooDeploymentName, err := helpers.GetGlooDeploymentName(opts.Top.Ctx, opts.Metadata.GetNamespace())
if err != nil {
return nil, err
}

options := []grpc.CallOption{
// Some proxies can become very large and exceed the default 100Mb limit
// For this reason we want remove the limit but will settle for a limit of MaxInt32
Expand All @@ -198,7 +203,7 @@ func getProxiesFromGrpc(name string, namespace string, opts *options.Options, pr
return nil, err
}
localPort := strconv.Itoa(freePort)
portFwdCmd, err := cliutil.PortForward(opts.Metadata.GetNamespace(), "deployment/gloo",
portFwdCmd, err := cliutil.PortForward(opts.Metadata.GetNamespace(), "deployment/"+glooDeploymentName,
localPort, proxyEndpointPort, opts.Top.Verbose)
if portFwdCmd.Process != nil {
defer portFwdCmd.Process.Release()
Expand Down
42 changes: 42 additions & 0 deletions projects/gloo/cli/pkg/helpers/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ var (
lock sync.Mutex
)

const (
GlooDeploymentName = "gloo"
)

// iterates over all the factory overrides, returning the first non-nil
// mem > consul
// if none set, return nil (callers will default to Kube CRD)
Expand Down Expand Up @@ -139,6 +143,44 @@ func KubeClient() (kubernetes.Interface, error) {
return clientset, nil
}

func GetGlooDeploymentName(ctx context.Context, namespace string) (string, error) {
client, err := KubeClient()
if err != nil {
errMessage := "error getting KubeClient"
fmt.Println(errMessage)
return "", fmt.Errorf(errMessage+": %v", err)
}
_, err = client.CoreV1().Namespaces().Get(ctx, namespace, metav1.GetOptions{})
if err != nil {
errMessage := "Gloo namespace does not exist"
fmt.Println(errMessage)
return "", fmt.Errorf(errMessage+": %v", err)
}
deployments, err := client.AppsV1().Deployments(namespace).List(ctx, metav1.ListOptions{
LabelSelector: "gloo=gloo",
})
if err != nil {
return "", err
}
if len(deployments.Items) == 1 {
return deployments.Items[0].Name, nil
}
errMessage := "Unable to find the gloo deployment"
// if there are multiple we can reasonably use the default variant
for _, d := range deployments.Items {
if d.Name != GlooDeploymentName {
// At least 1 deployment exists, in case we dont find default update our error message
errMessage = "too many app=gloo deployments, cannot decide which to target"
continue
}
// TODO: (nfuden) Remove this, while we should generally avoid println in our formatted output we already have alot of these
fmt.Println("multiple gloo labeled apps found, defaulting to", GlooDeploymentName)
return GlooDeploymentName, nil
}
fmt.Println(errMessage)
return "", fmt.Errorf(errMessage+": %v", err)
}

func MustGetNamespaces(ctx context.Context) []string {
ns, err := GetNamespaces(ctx)
if err != nil {
Expand Down
8 changes: 7 additions & 1 deletion projects/gloo/cli/pkg/xdsinspection/get_last_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
structpb "github.com/golang/protobuf/ptypes/struct"
"github.com/rotisserie/eris"
_ "github.com/solo-io/gloo/projects/envoyinit/hack/filter_types"
"github.com/solo-io/gloo/projects/gloo/cli/pkg/helpers"
"github.com/solo-io/gloo/projects/gloo/pkg/defaults"
"github.com/solo-io/go-utils/contextutils"
"go.uber.org/zap"
Expand All @@ -38,14 +39,19 @@ const (

func GetGlooXdsDump(ctx context.Context, proxyName, namespace string, verboseErrors bool) (*XdsDump, error) {

glooDeploymentName, err := helpers.GetGlooDeploymentName(ctx, namespace)
if err != nil {
return nil, err
}

xdsPort := strconv.Itoa(int(defaults.GlooXdsPort))
// If gloo is in MTLS mode
glooMtlsCheck := exec.Command("kubectl", "get", "configmap", envoySidecarConfig, "-n", namespace)
if err := glooMtlsCheck.Run(); err == nil {
xdsPort = strconv.Itoa(int(defaults.GlooMtlsModeXdsPort))
}
portFwd := exec.Command("kubectl", "port-forward", "-n", namespace,
"deployment/gloo", xdsPort)
"deployment/"+glooDeploymentName, xdsPort)
mergedPortForwardOutput := bytes.NewBuffer([]byte{})
portFwd.Stdout = mergedPortForwardOutput
portFwd.Stderr = mergedPortForwardOutput
Expand Down

0 comments on commit feb451a

Please sign in to comment.