From 8e7ea022f70320752d15ae0934ba458b212c1088 Mon Sep 17 00:00:00 2001 From: divolgin Date: Wed, 6 Jul 2022 15:40:06 -0700 Subject: [PATCH] Adding some utility interfaces for collectors --- .../v1beta2/collector_interfaces.go | 105 ++++++++++++++++++ .../troubleshoot/v1beta2/collector_shared.go | 18 +++ .../v1beta2/zz_generated.deepcopy.go | 48 ++++++++ pkg/collect/interfaces.go | 33 ++++++ pkg/collect/interfaces_test.go | 57 ++++++++++ 5 files changed, 261 insertions(+) create mode 100644 pkg/apis/troubleshoot/v1beta2/collector_interfaces.go create mode 100644 pkg/collect/interfaces.go create mode 100644 pkg/collect/interfaces_test.go diff --git a/pkg/apis/troubleshoot/v1beta2/collector_interfaces.go b/pkg/apis/troubleshoot/v1beta2/collector_interfaces.go new file mode 100644 index 00000000..302195eb --- /dev/null +++ b/pkg/apis/troubleshoot/v1beta2/collector_interfaces.go @@ -0,0 +1,105 @@ +package v1beta2 + +import ( + corev1 "k8s.io/api/core/v1" +) + +func (c *Run) GetImage() string { + return c.Image +} + +func (c *Run) SetImage(image string) { + c.Image = image +} + +func (c *Run) GetImagePullSecret() *ImagePullSecrets { + return c.ImagePullSecret +} + +func (c *Run) SetImagePullSecret(secrets *ImagePullSecrets) { + c.ImagePullSecret = secrets +} + +func (c *Run) GetNamespace() string { + return c.Namespace +} + +func (c *CopyFromHost) GetImage() string { + return c.Image +} + +func (c *CopyFromHost) SetImage(image string) { + c.Image = image +} + +func (c *CopyFromHost) GetImagePullSecret() *ImagePullSecrets { + return c.ImagePullSecret +} + +func (c *CopyFromHost) SetImagePullSecret(secrets *ImagePullSecrets) { + c.ImagePullSecret = secrets +} + +func (c *CopyFromHost) GetNamespace() string { + return c.Namespace +} + +func (c *Sysctl) GetImage() string { + return c.Image +} + +func (c *Sysctl) SetImage(image string) { + c.Image = image +} + +func (c *Sysctl) GetImagePullSecret() *ImagePullSecrets { + return c.ImagePullSecret +} + +func (c *Sysctl) SetImagePullSecret(secrets *ImagePullSecrets) { + c.ImagePullSecret = secrets +} + +func (c *Sysctl) GetNamespace() string { + return c.Namespace +} + +func (c *Collectd) GetImage() string { + return c.Image +} + +func (c *Collectd) SetImage(image string) { + c.Image = image +} + +func (c *Collectd) GetImagePullSecret() *ImagePullSecrets { + return c.ImagePullSecret +} + +func (c *Collectd) SetImagePullSecret(secrets *ImagePullSecrets) { + c.ImagePullSecret = secrets +} + +func (c *Collectd) GetNamespace() string { + return c.Namespace +} + +func (c *RunPod) GetPodSpec() corev1.PodSpec { + return c.PodSpec +} + +func (c *RunPod) SetPodSpec(podSpec corev1.PodSpec) { + c.PodSpec = podSpec +} + +func (c *RunPod) GetImagePullSecret() *ImagePullSecrets { + return c.ImagePullSecret +} + +func (c *RunPod) SetImagePullSecret(secrets *ImagePullSecrets) { + c.ImagePullSecret = secrets +} + +func (c *RunPod) GetNamespace() string { + return c.Namespace +} diff --git a/pkg/apis/troubleshoot/v1beta2/collector_shared.go b/pkg/apis/troubleshoot/v1beta2/collector_shared.go index fda95631..b2182ea8 100644 --- a/pkg/apis/troubleshoot/v1beta2/collector_shared.go +++ b/pkg/apis/troubleshoot/v1beta2/collector_shared.go @@ -2,6 +2,7 @@ package v1beta2 import ( "fmt" + "reflect" "strings" "github.com/replicatedhq/troubleshoot/pkg/multitype" @@ -538,3 +539,20 @@ func pickNamespaceOrDefault(collectorNS string, overrideNS string) string { } return "default" } + +func GetCollector(collector *Collect) interface{} { + if collector == nil { + return nil + } + + reflected := reflect.ValueOf(collector).Elem() + for i := 0; i < reflected.NumField(); i++ { + if reflected.Field(i).IsNil() { + continue + } + + return reflect.Indirect(reflected.Field(i)).Addr().Interface() + } + + return nil +} diff --git a/pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go b/pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go index 0862fdce..7fe8fdd2 100644 --- a/pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go +++ b/pkg/apis/troubleshoot/v1beta2/zz_generated.deepcopy.go @@ -355,6 +355,17 @@ func (in *AnalyzerSpec) DeepCopyInto(out *AnalyzerSpec) { } } } + if in.HostAnalyzers != nil { + in, out := &in.HostAnalyzers, &out.HostAnalyzers + *out = make([]*HostAnalyze, len(*in)) + for i := range *in { + if (*in)[i] != nil { + in, out := &(*in)[i], &(*out)[i] + *out = new(HostAnalyze) + (*in).DeepCopyInto(*out) + } + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new AnalyzerSpec. @@ -1637,6 +1648,11 @@ func (in *HostCollect) DeepCopyInto(out *HostCollect) { *out = new(HostOS) (*in).DeepCopyInto(*out) } + if in.HostRun != nil { + in, out := &in.HostRun, &out.HostRun + *out = new(HostRun) + (*in).DeepCopyInto(*out) + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HostCollect. @@ -1992,6 +2008,27 @@ func (in *HostPreflightStatus) DeepCopy() *HostPreflightStatus { return out } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *HostRun) DeepCopyInto(out *HostRun) { + *out = *in + in.HostCollectorMeta.DeepCopyInto(&out.HostCollectorMeta) + if in.Args != nil { + in, out := &in.Args, &out.Args + *out = make([]string, len(*in)) + copy(*out, *in) + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new HostRun. +func (in *HostRun) DeepCopy() *HostRun { + if in == nil { + return nil + } + out := new(HostRun) + in.DeepCopyInto(out) + return out +} + // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *HostServices) DeepCopyInto(out *HostServices) { *out = *in @@ -3792,6 +3829,17 @@ func (in *SupportBundleSpec) DeepCopyInto(out *SupportBundleSpec) { } } } + if in.HostAnalyzers != nil { + in, out := &in.HostAnalyzers, &out.HostAnalyzers + *out = make([]*HostAnalyze, len(*in)) + for i := range *in { + if (*in)[i] != nil { + in, out := &(*in)[i], &(*out)[i] + *out = new(HostAnalyze) + (*in).DeepCopyInto(*out) + } + } + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new SupportBundleSpec. diff --git a/pkg/collect/interfaces.go b/pkg/collect/interfaces.go new file mode 100644 index 00000000..f922b32c --- /dev/null +++ b/pkg/collect/interfaces.go @@ -0,0 +1,33 @@ +package collect + +import ( + "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" + corev1 "k8s.io/api/core/v1" +) + +type ImageRunner interface { + GetImage() string + SetImage(string) + + GetImagePullSecret() *v1beta2.ImagePullSecrets + SetImagePullSecret(*v1beta2.ImagePullSecrets) + + GetNamespace() string +} + +var _ ImageRunner = &v1beta2.Run{} +var _ ImageRunner = &v1beta2.CopyFromHost{} +var _ ImageRunner = &v1beta2.Sysctl{} +var _ ImageRunner = &v1beta2.Collectd{} + +type PodSpecRunner interface { + GetPodSpec() corev1.PodSpec + SetPodSpec(corev1.PodSpec) + + GetImagePullSecret() *v1beta2.ImagePullSecrets + SetImagePullSecret(*v1beta2.ImagePullSecrets) + + GetNamespace() string +} + +var _ PodSpecRunner = &v1beta2.RunPod{} diff --git a/pkg/collect/interfaces_test.go b/pkg/collect/interfaces_test.go new file mode 100644 index 00000000..a0566f18 --- /dev/null +++ b/pkg/collect/interfaces_test.go @@ -0,0 +1,57 @@ +package collect + +import ( + "testing" + + "github.com/replicatedhq/troubleshoot/pkg/apis/troubleshoot/v1beta2" + "github.com/stretchr/testify/require" +) + +func Test_interfaces(t *testing.T) { + runCollector := &v1beta2.Run{} + clusterInfoCollector := &v1beta2.ClusterInfo{} + + tests := []struct { + name string + collect *v1beta2.Collect + want interface{} + wantRunner bool + }{ + { + name: "image runner collector", + collect: &v1beta2.Collect{ + Run: runCollector, + }, + want: runCollector, + wantRunner: true, + }, + { + name: "not image runner collector", + collect: &v1beta2.Collect{ + ClusterInfo: clusterInfoCollector, + }, + want: clusterInfoCollector, + wantRunner: false, + }, + { + name: "no collector", + collect: &v1beta2.Collect{}, + want: nil, + wantRunner: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + req := require.New(t) + + got := v1beta2.GetCollector(tt.collect) + req.EqualValues(tt.want, got) + + runner, ok := got.(ImageRunner) + req.EqualValues(tt.wantRunner, ok) + if tt.wantRunner { + req.EqualValues(tt.want, runner) + } + }) + } +}