Skip to content

Commit

Permalink
fix some linting issues (more to go)
Browse files Browse the repository at this point in the history
Signed-off-by: Joni Collinge <jonathancollinge@live.com>
  • Loading branch information
jjcollinge committed Jan 15, 2022
1 parent e70c3e2 commit 2f24888
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 56 deletions.
8 changes: 4 additions & 4 deletions cmd/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var (
injectLogLevel string
injectAPITokenSecret string
injectAppTokenSecret string
injectLogAsJson bool
injectLogAsJSON bool
injectAppMaxConcurrency int
injectEnableMetrics bool
injectMetricsPort int
Expand Down Expand Up @@ -180,8 +180,8 @@ func getOptionsFromFlags() kubernetes.InjectOptions {
if injectAppTokenSecret != "" {
o = append(o, kubernetes.WithAppTokenSecret(injectAppTokenSecret))
}
if injectLogAsJson {
o = append(o, kubernetes.WithLogAsJson())
if injectLogAsJSON {
o = append(o, kubernetes.WithLogAsJSON())
}
if injectAppMaxConcurrency != -1 {
o = append(o, kubernetes.WithAppMaxConcurrency(injectAppMaxConcurrency))
Expand Down Expand Up @@ -266,7 +266,7 @@ func init() {
InjectCmd.Flags().StringVar(&injectLogLevel, "log-level", "", "The log level to use")
InjectCmd.Flags().StringVar(&injectAPITokenSecret, "api-token-secret", "", "The secret to use for the API token")
InjectCmd.Flags().StringVar(&injectAppTokenSecret, "app-token-secret", "", "The secret to use for the app token")
InjectCmd.Flags().BoolVar(&injectLogAsJson, "log-as-json", false, "Log as JSON")
InjectCmd.Flags().BoolVar(&injectLogAsJSON, "log-as-json", false, "Log as JSON")
InjectCmd.Flags().IntVar(&injectAppMaxConcurrency, "app-max-concurrency", -1, "The maximum number of concurrent requests to allow")
InjectCmd.Flags().BoolVar(&injectEnableMetrics, "enable-metrics", false, "Enable metrics")
InjectCmd.Flags().IntVar(&injectMetricsPort, "metrics-port", -1, "The port to expose the metrics on")
Expand Down
63 changes: 21 additions & 42 deletions pkg/kubernetes/injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
yamlDecoder "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/yaml"
)

Expand Down Expand Up @@ -68,6 +66,10 @@ const (
cronjob = "cronjob"
job = "job"
list = "list"

cronjobAnnotationsPath = "/spec/jobTemplate/spec/template/metadata/annotations"
podAnnotationsPath = "/metadata/annotations"
templateAnnotationsPath = "/spec/template/metadata/annotations"
)

type Injector interface {
Expand All @@ -88,7 +90,8 @@ type K8sInjectorConfig struct {

func NewK8sInjector(config K8sInjectorConfig) *K8sInjector {
return &K8sInjector{
config: config,
config: config,
injected: false,
}
}

Expand Down Expand Up @@ -154,7 +157,7 @@ func (p *K8sInjector) processInput(input io.Reader, out io.Writer, opts InjectOp
if err != nil {
return err
}
items = append(items, runtime.RawExtension{Raw: injectedJSON})
items = append(items, runtime.RawExtension{Raw: injectedJSON}) // nolint:exhaustivestruct
}
sourceList.Items = items
result, err := yaml.Marshal(sourceList)
Expand Down Expand Up @@ -218,61 +221,61 @@ func (p *K8sInjector) injectYAML(input []byte, config InjectOptions) ([]byte, bo
var name string
switch strings.ToLower(metaType.Kind) {
case pod:
pod := &corev1.Pod{}
pod := &corev1.Pod{} // nolint:exhaustivestruct
if err := yaml.Unmarshal(input, pod); err != nil {
return nil, false, err
}
name = pod.Name
annotations = pod.Annotations
path = "/metadata/annotations"
path = podAnnotationsPath
case cronjob:
cronjob := &batchv1beta1.CronJob{}
cronjob := &batchv1beta1.CronJob{} // nolint:exhaustivestruct
if err := yaml.Unmarshal(input, cronjob); err != nil {
return nil, false, err
}
name = cronjob.Name
annotations = cronjob.Spec.JobTemplate.Spec.Template.Annotations
path = "/spec/jobTemplate/spec/template/metadata/annotations"
path = cronjobAnnotationsPath
case deployment:
deployment := &appsv1.Deployment{}
deployment := &appsv1.Deployment{} // nolint:exhaustivestruct
if err := yaml.Unmarshal(input, deployment); err != nil {
return nil, false, err
}
name = deployment.Name
annotations = deployment.Spec.Template.Annotations
path = "/spec/template/metadata/annotations"
path = templateAnnotationsPath
case replicaset:
replicaset := &appsv1.ReplicaSet{}
replicaset := &appsv1.ReplicaSet{} // nolint:exhaustivestruct
if err := yaml.Unmarshal(input, replicaset); err != nil {
return nil, false, err
}
name = replicaset.Name
annotations = replicaset.Spec.Template.Annotations
path = "/spec/template/metadata/annotations"
path = templateAnnotationsPath
case job:
job := &batchv1.Job{}
job := &batchv1.Job{} // nolint:exhaustivestruct
if err := yaml.Unmarshal(input, job); err != nil {
return nil, false, err
}
name = job.Name
annotations = job.Spec.Template.Annotations
path = "/spec/template/metadata/annotations"
path = templateAnnotationsPath
case statefulset:
statefulset := &appsv1.StatefulSet{}
statefulset := &appsv1.StatefulSet{} // nolint:exhaustivestruct
if err := yaml.Unmarshal(input, statefulset); err != nil {
return nil, false, err
}
name = statefulset.Name
annotations = statefulset.Spec.Template.Annotations
path = "/spec/template/metadata/annotations"
path = templateAnnotationsPath
case daemonset:
daemonset := &appsv1.DaemonSet{}
daemonset := &appsv1.DaemonSet{} // nolint:exhaustivestruct
if err := yaml.Unmarshal(input, daemonset); err != nil {
return nil, false, err
}
name = daemonset.Name
annotations = daemonset.Spec.Template.Annotations
path = "/spec/template/metadata/annotations"
path = templateAnnotationsPath
default:
// No injection needed for this kind.
return input, false, nil
Expand Down Expand Up @@ -436,27 +439,3 @@ func getDaprAnnotations(config *InjectOptions) map[string]string {

return annotations
}

func getResourceObjectAndGKV(in io.Reader) (runtime.Object, *schema.GroupVersionKind, error) {
reader := yamlDecoder.NewYAMLReader(bufio.NewReaderSize(in, 4096))
bytes, err := reader.Read()
if err != nil {
return nil, nil, err
}

return scheme.Codecs.UniversalDeserializer().Decode(bytes, nil, nil)
}

func getAnnotationsFromDeployment(deployment *appsv1.Deployment) (map[string]string, error) {
if deployment.Spec.Template.ObjectMeta.Annotations == nil {
deployment.Spec.Template.ObjectMeta.Annotations = make(map[string]string)
}
return deployment.Spec.Template.ObjectMeta.Annotations, nil
}

func getAnnotationsFromPod(pod *corev1.Pod) (map[string]string, error) {
if pod.Annotations == nil {
pod.Annotations = make(map[string]string)
}
return pod.Annotations, nil
}
4 changes: 2 additions & 2 deletions pkg/kubernetes/injector_config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package kubernetes

// InjectOptions configurate the injection behavior.
// InjectOptions configure the injection behavior.
type InjectOptions struct {
appID *string
metricsEnabled *bool
Expand Down Expand Up @@ -109,7 +109,7 @@ func WithAppTokenSecret(appTokenSecret string) InjectOption {
}
}

func WithLogAsJson() InjectOption {
func WithLogAsJSON() InjectOption {
return func(config *InjectOptions) {
enabled := true
config.logAsJson = &enabled
Expand Down
16 changes: 8 additions & 8 deletions pkg/kubernetes/injector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestInject(t *testing.T) {
expectedFilePath string
}{
{
testID: "single targetted injection into pod config 1",
testID: "single targeted injection into pod config 1",
injections: []injection{
{
targetName: "mypod",
Expand All @@ -40,7 +40,7 @@ func TestInject(t *testing.T) {
expectedFilePath: "testdata/pod_injected_conf_1.yml",
},
{
testID: "single targetted injection into pod config 2",
testID: "single targeted injection into pod config 2",
injections: []injection{
{
targetName: "mypod",
Expand All @@ -58,7 +58,7 @@ func TestInject(t *testing.T) {
expectedFilePath: "testdata/pod_injected_conf_2.yml",
},
{
testID: "single targetted injection into deployment config 1",
testID: "single targeted injection into deployment config 1",
injections: []injection{
{
targetName: "nodeapp",
Expand Down Expand Up @@ -90,7 +90,7 @@ func TestInject(t *testing.T) {
expectedFilePath: "testdata/deployment_injected_conf_1.yml",
},
{
testID: "single targetted injection into multi config 1",
testID: "single targeted injection into multi config 1",
injections: []injection{
{
targetName: "divideapp",
Expand All @@ -107,7 +107,7 @@ func TestInject(t *testing.T) {
expectedFilePath: "testdata/multi_injected_conf_1.yml",
},
{
testID: "multiple targetted injections into multi config 2",
testID: "multiple targeted injections into multi config 2",
injections: []injection{
{
targetName: "subtractapp",
Expand Down Expand Up @@ -164,7 +164,7 @@ func TestInject(t *testing.T) {
expectedFilePath: "testdata/multi_injected_conf_2.yml",
},
{
testID: "single untargetted injection into multi config 3",
testID: "single untargeted injection into multi config 3",
injections: []injection{
{
optionFactory: func() InjectOptions {
Expand All @@ -180,7 +180,7 @@ func TestInject(t *testing.T) {
expectedFilePath: "testdata/multi_injected_conf_3.yml",
},
{
testID: "single untargetted injection into list config",
testID: "single untargeted injection into list config",
injections: []injection{
{
optionFactory: func() InjectOptions {
Expand Down Expand Up @@ -282,7 +282,7 @@ func TestGetDaprAnnotations(t *testing.T) {
WithConfig("test-config"),
WithDebugEnabled(),
WithEnv("key=value,key1=value1"),
WithLogAsJson(),
WithLogAsJSON(),
WithListenAddresses("0.0.0.0"),
WithDaprImage("test-image"),
WithProfileEnabled(),
Expand Down

0 comments on commit 2f24888

Please sign in to comment.