Skip to content

Commit

Permalink
Add outputs for OpenShift clusters (#5)
Browse files Browse the repository at this point in the history
* Remove agentns flag from inspect
* Add server version to query
* Fix dependabot alerts
* Bump k8s.io to v0.24.10
  • Loading branch information
nfisher authored Feb 22, 2023
1 parent d132d21 commit ad0839b
Show file tree
Hide file tree
Showing 8 changed files with 347 additions and 137 deletions.
27 changes: 11 additions & 16 deletions cluster/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cluster
import (
"fmt"
"strconv"
"strings"
)

// NewIndex builds a new empty index for PodInfo.
Expand Down Expand Up @@ -121,6 +120,7 @@ func (index *Index) EachPod(pod PodInfo) {
}
index.Containers.Add(fmt.Sprintf("%s/%s", qualifiedName, name))
}

for n, t := range pod.Owners {
switch t {
case DaemonSet:
Expand Down Expand Up @@ -156,22 +156,17 @@ func IsInstanaAgent(pod PodInfo) bool {
}

func IsCNIPlugin(n string) bool {
if n == "aws-node" {
return true
}
if strings.HasPrefix(n, "cilium") {
return true
}
if strings.HasPrefix(n, "calico") {
return true
}
if strings.HasPrefix(n, "flannel") {
return true
m := map[string]bool{
"aws-node": true,
"calico": true,
"cilium": true,
"flannel": true,
"kube-router": true,
"multus": true,
"sdn": true,
}
if strings.HasPrefix(n, "kube-router") {
return true
}
return false

return m[n]
}

type Counter map[string]int
Expand Down
17 changes: 9 additions & 8 deletions cluster/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ type Applyable interface {

// Info is a data structure for relevant cluster data.
type Info struct {
Name string
NodeCount int
Nodes []NodeInfo
PodCount int
Pods []PodInfo
Version string
Started time.Time
Finished time.Time
Name string
NodeCount int
Nodes []NodeInfo
PodCount int
Pods []PodInfo
ServerVersion string
Version string
Started time.Time
Finished time.Time
}

// Apply iterates over each pod and yields it to the list of applyables.
Expand Down
25 changes: 18 additions & 7 deletions cluster/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"k8s.io/apimachinery/pkg/version"
"time"

v1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -37,7 +38,7 @@ func New(kubeconfig string) (*KubernetesQuery, error) {
return nil, err
}

return NewQuery(config.Host, clientset.CoreV1(), clientset.AppsV1()), nil
return NewQuery(config.Host, clientset.CoreV1(), clientset.AppsV1(), clientset.ServerVersion), nil
}

// Query is a query interface for the cluster.
Expand All @@ -46,20 +47,22 @@ type Query interface {
AllPods() ([]PodInfo, error)
AllNodes() ([]NodeInfo, error)
Host() string
Time() time.Time
InstanaLeader() (string, error)
ServerVersion() (string, error)
Time() time.Time
}

// NewQuery allocates and returns a new Query.
func NewQuery(h string, cs typev1.CoreV1Interface, apps appv1.AppsV1Interface) *KubernetesQuery {
return &KubernetesQuery{h, cs, apps}
func NewQuery(h string, cs typev1.CoreV1Interface, apps appv1.AppsV1Interface, version func() (*version.Info, error)) *KubernetesQuery {
return &KubernetesQuery{h, cs, apps, version}
}

// KubernetesQuery is a concrete Kubernetes client to query various cluster info.
type KubernetesQuery struct {
host string
core typev1.CoreV1Interface
apps appv1.AppsV1Interface
host string
core typev1.CoreV1Interface
apps appv1.AppsV1Interface
version func() (*version.Info, error)
}

// Time returns the current time.
Expand All @@ -72,6 +75,14 @@ func (q *KubernetesQuery) Host() string {
return q.host
}

func (q *KubernetesQuery) ServerVersion() (string, error) {
info, err := q.version()
if err != nil {
return "", err
}
return info.GitVersion, nil
}

// InstanaLeader returns the instana agent leader pod name.
func (q *KubernetesQuery) InstanaLeader() (string, error) {
ep, err := q.core.Endpoints("default").Get(context.TODO(), "instana", metav1.GetOptions{})
Expand Down
12 changes: 6 additions & 6 deletions cluster/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func Test_InstanaLeader_should_return_leader(t *testing.T) {
t.Parallel()
endpoints := v1.EndpointsList{Items: []v1.Endpoints{instanaEndpoint(`{"holderIdentity":"instana-agent-hcdhs"}`)}}
client := fake.NewSimpleClientset(&endpoints)
query := cluster.NewQuery("localhost:1234", client.CoreV1(), client.AppsV1())
query := cluster.NewQuery("localhost:1234", client.CoreV1(), client.AppsV1(), nil)
leader, err := query.InstanaLeader()
if err != nil {
t.Errorf("query.InstanaLeader() err=%#v, want nil", err)
Expand All @@ -30,7 +30,7 @@ func Test_InstanaLeader_should_return_invalid_format_if_json_invalid(t *testing.
t.Parallel()
endpoints := v1.EndpointsList{Items: []v1.Endpoints{instanaEndpoint("foobar")}}
client := fake.NewSimpleClientset(&endpoints)
query := cluster.NewQuery("localhost:1234", client.CoreV1(), client.AppsV1())
query := cluster.NewQuery("localhost:1234", client.CoreV1(), client.AppsV1(), nil)
_, err := query.InstanaLeader()
if err != cluster.ErrInvalidLeaseFormat {
t.Errorf("query.InstanaLeader() err=%#v, want ErrInvalidLeaseFormat", err)
Expand All @@ -41,7 +41,7 @@ func Test_InstanaLeader_should_return_leader_unknown_if_none_defined(t *testing.
t.Parallel()
endpoints := v1.EndpointsList{Items: []v1.Endpoints{instanaEndpoint("")}}
client := fake.NewSimpleClientset(&endpoints)
query := cluster.NewQuery("localhost:1234", client.CoreV1(), client.AppsV1())
query := cluster.NewQuery("localhost:1234", client.CoreV1(), client.AppsV1(), nil)
_, err := query.InstanaLeader()
if err != cluster.ErrLeaderUndefined {
t.Errorf("query.InstanaLeader() err=%#v, want ErrLeaderUndefined", err)
Expand All @@ -52,7 +52,7 @@ func Test_InstanaLeader_should_error_when_no_endpoint(t *testing.T) {
t.Parallel()
endpoints := v1.EndpointsList{Items: []v1.Endpoints{}}
client := fake.NewSimpleClientset(&endpoints)
query := cluster.NewQuery("localhost:1234", client.CoreV1(), client.AppsV1())
query := cluster.NewQuery("localhost:1234", client.CoreV1(), client.AppsV1(), nil)
_, err := query.InstanaLeader()
_, ok := err.(*errors.StatusError)
if !ok {
Expand All @@ -64,7 +64,7 @@ func Test_AllNodes(t *testing.T) {
t.Parallel()
items := []v1.Node{awsHost()}
client := fake.NewSimpleClientset(&v1.NodeList{Items: items})
query := cluster.NewQuery("localhost:1234", client.CoreV1(), client.AppsV1())
query := cluster.NewQuery("localhost:1234", client.CoreV1(), client.AppsV1(), nil)

all, err := query.AllNodes()
if err != nil {
Expand Down Expand Up @@ -92,7 +92,7 @@ func Test_AllPods(t *testing.T) {

client := fake.NewSimpleClientset(&v1.PodList{Items: items})

query := cluster.NewQuery("localhost:1234", client.CoreV1(), client.AppsV1())
query := cluster.NewQuery("localhost:1234", client.CoreV1(), client.AppsV1(), nil)

all, err := query.AllPods()
if err != nil {
Expand Down
33 changes: 33 additions & 0 deletions cmd/envcheckctl/exec_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"os"
"sort"
"strings"
"time"

"github.com/instana/envcheck/agent"
Expand Down Expand Up @@ -71,6 +72,7 @@ func ExecInspect(config EnvcheckConfig) {
info.Finished.Sub(info.Started))
log.Printf("coverage=\"%d of %d (%0.2f%%)\"\n\n", index.AgentRestarts.Len(), index.Nodes.Len(), float64(index.AgentRestarts.Len())/float64(index.Nodes.Len())*100.0)

PrintKind(info.ServerVersion)
PrintTop(10, "agentRestarts", index.AgentRestarts)
PrintCounter("agentStatus", index.AgentStatus)
PrintCounter("chartVersions", index.ChartVersions)
Expand All @@ -93,6 +95,28 @@ func ExecInspect(config EnvcheckConfig) {
size.Heap)
}

func PrintKind(version string) {
dist := ExtractDistribution(version)
log.Println("serverDistribution:")
log.Println(" -", dist)
log.Println("")
log.Println("serverVersion:")
log.Println(" -", version)
log.Println("")
}

func ExtractDistribution(version string) string {
distribution := "kubernetes"
if strings.Contains(version, "gke") {
distribution = "openshift"
} else if strings.Contains(version, "gke") {
distribution = "gke"
} else if strings.Contains(version, "eks") {
distribution = "eks"
}
return distribution
}

type top struct {
name string
value int
Expand Down Expand Up @@ -126,6 +150,9 @@ func PrintCounter(header string, c cluster.Counter) {
for _, k := range keys {
log.Printf("- \"%v\"=%d", k, c[k])
}
if len(keys) == 0 {
log.Println(" - \"no known resource found\"")
}
}

// QueryLive queries a cluster and builds the cluster info from the current data.
Expand All @@ -137,6 +164,12 @@ func QueryLive(query cluster.Query) (*cluster.Info, error) {

log.Printf("envcheckctl=%s, cluster=%v, start=%v\n", Revision, info.Name, info.Started.Format(time.RFC3339))
log.Println("Collecting cluster details. Duration varies depending on the cluster.")
versionInfo, err := query.ServerVersion()
if err != nil {
return nil, err
}
info.ServerVersion = versionInfo

pods, err := query.AllPods()
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions cmd/envcheckctl/exec_inspect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ type stubQuery struct {
ts time.Time
}

func (q *stubQuery) ServerVersion() (string, error) {
return "v1.23.14-eks-ffeb93d", nil
}

func (q *stubQuery) InstanaLeader() (string, error) {
return "instana-agent-hcdhs", nil
}
Expand Down
59 changes: 35 additions & 24 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,52 +1,63 @@
module github.com/instana/envcheck

go 1.18
go 1.19

require (
github.com/google/go-cmp v0.5.5
github.com/jackpal/gateway v1.0.6
k8s.io/api v0.22.9
k8s.io/apimachinery v0.22.9
k8s.io/client-go v0.22.9
k8s.io/api v0.24.10
k8s.io/apimachinery v0.24.10
k8s.io/client-go v0.24.10
)

require (
cloud.google.com/go v0.54.0 // indirect
cloud.google.com/go v0.81.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.11.18 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.13 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/evanphx/json-patch v4.11.0+incompatible // indirect
github.com/emicklei/go-restful v2.9.5+incompatible // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect
github.com/go-logr/logr v0.4.0 // indirect
github.com/go-logr/logr v1.2.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.19.5 // indirect
github.com/go-openapi/swag v0.19.14 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/gnostic v0.5.7-v3refs // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/imdario/mergo v0.3.5 // indirect
github.com/json-iterator/go v1.1.11 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.6 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22 // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
google.golang.org/appengine v1.6.5 // indirect
google.golang.org/protobuf v1.26.0 // indirect
golang.org/x/crypto v0.6.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/time v0.0.0-20220210224613-90d013bbcef8 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
k8s.io/klog/v2 v2.9.0 // indirect
k8s.io/kube-openapi v0.0.0-20211109043538-20434351676c // indirect
k8s.io/utils v0.0.0-20211116205334-6203023598ed // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.60.1 // indirect
k8s.io/kube-openapi v0.0.0-20220328201542-3ee0da9b0b42 // indirect
k8s.io/utils v0.0.0-20220210201930-3a6ce19ff2f9 // indirect
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.2.0 // indirect
)
Loading

0 comments on commit ad0839b

Please sign in to comment.