|
1 | 1 | package v1beta2 |
2 | 2 |
|
3 | | -// Hub marks this type as a conversion hub. |
4 | | -func (*Instrumentation) Hub() {} |
| 3 | +import ( |
| 4 | + "github.com/newrelic/k8s-agents-operator/api/current" |
| 5 | + "sigs.k8s.io/controller-runtime/pkg/conversion" |
| 6 | +) |
| 7 | + |
| 8 | +// ConvertTo converts this Instrumentation to the Hub version. |
| 9 | +func (src *Instrumentation) ConvertTo(dstRaw conversion.Hub) error { |
| 10 | + dst := dstRaw.(*current.Instrumentation) |
| 11 | + |
| 12 | + // ObjectMeta |
| 13 | + dst.ObjectMeta = src.ObjectMeta |
| 14 | + |
| 15 | + for _, srcExpr := range src.Spec.ContainerSelector.ImageSelector.MatchExpressions { |
| 16 | + dst.Spec.ContainerSelector.ImageSelector.MatchExpressions = append(dst.Spec.ContainerSelector.ImageSelector.MatchExpressions, current.ImageSelectorRequirement{ |
| 17 | + Key: current.ImageSelectorKey(srcExpr.Key), Operator: current.ImageSelectorOperator(srcExpr.Operator), Values: srcExpr.Values, |
| 18 | + }) |
| 19 | + } |
| 20 | + for _, srcExpr := range src.Spec.ContainerSelector.EnvSelector.MatchExpressions { |
| 21 | + dst.Spec.ContainerSelector.EnvSelector.MatchExpressions = append(dst.Spec.ContainerSelector.EnvSelector.MatchExpressions, current.EnvSelectorRequirement{ |
| 22 | + Key: srcExpr.Key, Operator: current.EnvSelectorOperator(srcExpr.Operator), Values: srcExpr.Values, |
| 23 | + }) |
| 24 | + } |
| 25 | + for _, srcExpr := range src.Spec.ContainerSelector.NameSelector.MatchExpressions { |
| 26 | + dst.Spec.ContainerSelector.NameSelector.MatchExpressions = append(dst.Spec.ContainerSelector.NameSelector.MatchExpressions, current.NameSelectorRequirement{ |
| 27 | + Key: current.NameSelectorKey(srcExpr.Key), Operator: current.NameSelectorOperator(srcExpr.Operator), Values: srcExpr.Values, |
| 28 | + }) |
| 29 | + } |
| 30 | + |
| 31 | + dst.Spec.ContainerSelector.ImageSelector.MatchImages = src.Spec.ContainerSelector.ImageSelector.MatchImages |
| 32 | + dst.Spec.ContainerSelector.EnvSelector.MatchEnvs = src.Spec.ContainerSelector.EnvSelector.MatchEnvs |
| 33 | + dst.Spec.ContainerSelector.NameSelector.MatchNames = src.Spec.ContainerSelector.NameSelector.MatchNames |
| 34 | + dst.Spec.ContainerSelector.NamesFromPodAnnotation = src.Spec.ContainerSelector.NamesFromPodAnnotation |
| 35 | + |
| 36 | + dst.Spec.PodLabelSelector = src.Spec.PodLabelSelector |
| 37 | + dst.Spec.NamespaceLabelSelector = src.Spec.NamespaceLabelSelector |
| 38 | + dst.Spec.LicenseKeySecret = src.Spec.LicenseKeySecret |
| 39 | + dst.Spec.AgentConfigMap = src.Spec.AgentConfigMap |
| 40 | + dst.Spec.Agent = current.Agent{ |
| 41 | + Language: src.Spec.Agent.Language, |
| 42 | + Image: src.Spec.Agent.Image, |
| 43 | + VolumeSizeLimit: src.Spec.Agent.VolumeSizeLimit, |
| 44 | + Env: src.Spec.Agent.Env, |
| 45 | + Resources: src.Spec.Agent.Resources, |
| 46 | + ImagePullPolicy: src.Spec.Agent.ImagePullPolicy, |
| 47 | + SecurityContext: src.Spec.Agent.SecurityContext, |
| 48 | + } |
| 49 | + dst.Spec.HealthAgent = current.HealthAgent{ |
| 50 | + Image: src.Spec.HealthAgent.Image, |
| 51 | + Env: src.Spec.HealthAgent.Env, |
| 52 | + ImagePullPolicy: src.Spec.HealthAgent.ImagePullPolicy, |
| 53 | + SecurityContext: src.Spec.HealthAgent.SecurityContext, |
| 54 | + Resources: src.Spec.HealthAgent.Resources, |
| 55 | + } |
| 56 | + var unhealthyPodErrors []current.UnhealthyPodError |
| 57 | + if l := len(src.Status.UnhealthyPodsErrors); l > 0 { |
| 58 | + unhealthyPodErrors = make([]current.UnhealthyPodError, l) |
| 59 | + for i, e := range src.Status.UnhealthyPodsErrors { |
| 60 | + unhealthyPodErrors[i] = current.UnhealthyPodError{ |
| 61 | + Pod: e.Pod, |
| 62 | + LastError: e.LastError, |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + dst.Status = current.InstrumentationStatus{ |
| 67 | + PodsMatching: src.Status.PodsMatching, |
| 68 | + PodsUnhealthy: src.Status.PodsUnhealthy, |
| 69 | + PodsHealthy: src.Status.PodsHealthy, |
| 70 | + PodsInjected: src.Status.PodsInjected, |
| 71 | + PodsOutdated: src.Status.PodsOutdated, |
| 72 | + PodsNotReady: src.Status.PodsNotReady, |
| 73 | + UnhealthyPodsErrors: unhealthyPodErrors, |
| 74 | + LastUpdated: src.Status.LastUpdated, |
| 75 | + ObservedVersion: src.Status.ObservedVersion, |
| 76 | + } |
| 77 | + return nil |
| 78 | +} |
| 79 | + |
| 80 | +// ConvertFrom converts from the Hub version to this version. |
| 81 | +func (dst *Instrumentation) ConvertFrom(srcRaw conversion.Hub) error { |
| 82 | + src := srcRaw.(*current.Instrumentation) |
| 83 | + |
| 84 | + // ObjectMeta |
| 85 | + dst.ObjectMeta = src.ObjectMeta |
| 86 | + |
| 87 | + // Spec |
| 88 | + |
| 89 | + for _, srcExpr := range src.Spec.ContainerSelector.ImageSelector.MatchExpressions { |
| 90 | + dst.Spec.ContainerSelector.ImageSelector.MatchExpressions = append(dst.Spec.ContainerSelector.ImageSelector.MatchExpressions, ImageSelectorRequirement{ |
| 91 | + Key: ImageSelectorKey(srcExpr.Key), Operator: ImageSelectorOperator(srcExpr.Operator), Values: srcExpr.Values, |
| 92 | + }) |
| 93 | + } |
| 94 | + for _, srcExpr := range src.Spec.ContainerSelector.EnvSelector.MatchExpressions { |
| 95 | + dst.Spec.ContainerSelector.EnvSelector.MatchExpressions = append(dst.Spec.ContainerSelector.EnvSelector.MatchExpressions, EnvSelectorRequirement{ |
| 96 | + Key: srcExpr.Key, Operator: EnvSelectorOperator(srcExpr.Operator), Values: srcExpr.Values, |
| 97 | + }) |
| 98 | + } |
| 99 | + for _, srcExpr := range src.Spec.ContainerSelector.NameSelector.MatchExpressions { |
| 100 | + dst.Spec.ContainerSelector.NameSelector.MatchExpressions = append(dst.Spec.ContainerSelector.NameSelector.MatchExpressions, NameSelectorRequirement{ |
| 101 | + Key: NameSelectorKey(srcExpr.Key), Operator: NameSelectorOperator(srcExpr.Operator), Values: srcExpr.Values, |
| 102 | + }) |
| 103 | + } |
| 104 | + |
| 105 | + dst.Spec.ContainerSelector.ImageSelector.MatchImages = src.Spec.ContainerSelector.ImageSelector.MatchImages |
| 106 | + dst.Spec.ContainerSelector.EnvSelector.MatchEnvs = src.Spec.ContainerSelector.EnvSelector.MatchEnvs |
| 107 | + dst.Spec.ContainerSelector.NameSelector.MatchNames = src.Spec.ContainerSelector.NameSelector.MatchNames |
| 108 | + dst.Spec.ContainerSelector.NamesFromPodAnnotation = src.Spec.ContainerSelector.NamesFromPodAnnotation |
| 109 | + |
| 110 | + dst.Spec.PodLabelSelector = src.Spec.PodLabelSelector |
| 111 | + dst.Spec.NamespaceLabelSelector = src.Spec.NamespaceLabelSelector |
| 112 | + dst.Spec.LicenseKeySecret = src.Spec.LicenseKeySecret |
| 113 | + dst.Spec.Agent = Agent{ |
| 114 | + Env: src.Spec.Agent.Env, |
| 115 | + Image: src.Spec.Agent.Image, |
| 116 | + ImagePullPolicy: src.Spec.Agent.ImagePullPolicy, |
| 117 | + Language: src.Spec.Agent.Language, |
| 118 | + Resources: src.Spec.Agent.Resources, |
| 119 | + SecurityContext: src.Spec.Agent.SecurityContext, |
| 120 | + VolumeSizeLimit: src.Spec.Agent.VolumeSizeLimit, |
| 121 | + } |
| 122 | + dst.Spec.HealthAgent = HealthAgent{ |
| 123 | + Env: src.Spec.HealthAgent.Env, |
| 124 | + Image: src.Spec.HealthAgent.Image, |
| 125 | + ImagePullPolicy: src.Spec.HealthAgent.ImagePullPolicy, |
| 126 | + Resources: src.Spec.HealthAgent.Resources, |
| 127 | + SecurityContext: src.Spec.HealthAgent.SecurityContext, |
| 128 | + } |
| 129 | + dst.Spec.AgentConfigMap = src.Spec.AgentConfigMap |
| 130 | + var unhealthyPodErrors []UnhealthyPodError |
| 131 | + if l := len(src.Status.UnhealthyPodsErrors); l > 0 { |
| 132 | + unhealthyPodErrors = make([]UnhealthyPodError, l) |
| 133 | + for i, e := range src.Status.UnhealthyPodsErrors { |
| 134 | + unhealthyPodErrors[i] = UnhealthyPodError{ |
| 135 | + Pod: e.Pod, |
| 136 | + LastError: e.LastError, |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + dst.Status = InstrumentationStatus{ |
| 141 | + PodsMatching: src.Status.PodsMatching, |
| 142 | + PodsUnhealthy: src.Status.PodsUnhealthy, |
| 143 | + PodsHealthy: src.Status.PodsHealthy, |
| 144 | + PodsInjected: src.Status.PodsInjected, |
| 145 | + PodsOutdated: src.Status.PodsOutdated, |
| 146 | + PodsNotReady: src.Status.PodsNotReady, |
| 147 | + UnhealthyPodsErrors: unhealthyPodErrors, |
| 148 | + LastUpdated: src.Status.LastUpdated, |
| 149 | + ObservedVersion: src.Status.ObservedVersion, |
| 150 | + } |
| 151 | + return nil |
| 152 | +} |
0 commit comments