Skip to content

Commit 8c301f8

Browse files
authored
fix(k8s): Fix inet-type panic (#6841)
Make our resolvers ignore `None` strings. The other way to fix is to just leave the incoming field as a `string`. This way is less breaking for users.
1 parent 795e40d commit 8c301f8

3 files changed

Lines changed: 60 additions & 9 deletions

File tree

plugins/source/k8s/client/helpers.go

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,34 +52,85 @@ func ResolveContext(_ context.Context, meta schema.ClientMeta, r *schema.Resourc
5252
return r.Set(c.Name, client.Context)
5353
}
5454

55-
// In k8s, IP Addresses may sometimes be empty-strings - but postgresql doesn't like that.
56-
// So, the resolver for ip-addresses should recognize that case and not set null instead.
55+
// In k8s, IP Addresses may sometimes be empty-strings or `None` - but postgresql doesn't like that.
56+
// So, the resolver for ip-addresses should recognize that case and set null instead.
5757
func StringToInetPathResolver(path string) schema.ColumnResolver {
5858
return func(_ context.Context, meta schema.ClientMeta, r *schema.Resource, c schema.Column) error {
5959
value := funk.Get(r.Item, path, funk.WithAllowZero())
6060

6161
stringValue, ok := value.(string)
62-
if ok && stringValue != "" {
62+
if ok && stringValue != "" && stringValue != "None" {
6363
return r.Set(c.Name, value)
6464
}
65+
6566
return r.Set(c.Name, nil)
6667
}
6768
}
6869

69-
// In k8s, IP Addresses may sometimes be empty-strings - but postgresql doesn't like that.
70-
// So, the resolver for ip-addresses should recognize that case and not set null instead.
70+
// In k8s, IP Addresses may sometimes be empty-strings or `None` - but postgresql doesn't like that.
71+
// So, the resolver for ip-addresses should recognize that case and set null instead.
72+
func StringToInetArrayPathResolver(path string) schema.ColumnResolver {
73+
return func(_ context.Context, meta schema.ClientMeta, r *schema.Resource, c schema.Column) error {
74+
value := funk.Get(r.Item, path, funk.WithAllowZero())
75+
76+
stringArrayValue, ok := value.([]string)
77+
if !ok {
78+
return r.Set(c.Name, nil)
79+
}
80+
81+
sanitized := []*string{}
82+
83+
for i := range stringArrayValue {
84+
if stringArrayValue[i] != "" && stringArrayValue[i] != "None" {
85+
sanitized = append(sanitized, &stringArrayValue[i])
86+
} else {
87+
sanitized = append(sanitized, nil)
88+
}
89+
}
90+
91+
return r.Set(c.Name, sanitized)
92+
}
93+
}
94+
95+
// In k8s, IP Addresses may sometimes be empty-strings or `None` - but postgresql doesn't like that.
96+
// So, the resolver for ip-addresses should recognize that case and set null instead.
7197
func StringToCidrPathResolver(path string) schema.ColumnResolver {
7298
return func(_ context.Context, meta schema.ClientMeta, r *schema.Resource, c schema.Column) error {
7399
value := funk.Get(r.Item, path, funk.WithAllowZero())
74100

75101
stringValue, ok := value.(string)
76-
if ok && stringValue != "" {
102+
if ok && stringValue != "" && stringValue != "None" {
77103
return r.Set(c.Name, value)
78104
}
79105
return r.Set(c.Name, nil)
80106
}
81107
}
82108

109+
// In k8s, IP Addresses may sometimes be empty-strings or `None` - but postgresql doesn't like that.
110+
// So, the resolver for ip-addresses should recognize that case and set null instead.
111+
func StringToCidrArrayPathResolver(path string) schema.ColumnResolver {
112+
return func(_ context.Context, meta schema.ClientMeta, r *schema.Resource, c schema.Column) error {
113+
value := funk.Get(r.Item, path, funk.WithAllowZero())
114+
115+
stringArrayValue, ok := value.([]string)
116+
if !ok {
117+
return r.Set(c.Name, nil)
118+
}
119+
120+
sanitized := []*string{}
121+
122+
for i := range stringArrayValue {
123+
if stringArrayValue[i] != "" && stringArrayValue[i] != "None" {
124+
sanitized = append(sanitized, &stringArrayValue[i])
125+
} else {
126+
sanitized = append(sanitized, nil)
127+
}
128+
}
129+
130+
return r.Set(c.Name, sanitized)
131+
}
132+
}
133+
83134
// isK8sTimeStruct returns true if the given type is a metav1.Time struct or a pointer to it.
84135
func isK8sTimeStruct(fieldType reflect.Type) bool {
85136
fieldKind := fieldType.Kind()

plugins/source/k8s/resources/services/core/nodes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func Nodes() *schema.Table {
4141
{
4242
Name: "spec_pod_cidrs",
4343
Type: schema.TypeCIDRArray,
44-
Resolver: schema.PathResolver("Spec.PodCIDRs"),
44+
Resolver: client.StringToCidrArrayPathResolver("Spec.PodCIDRs"),
4545
},
4646
},
4747
}

plugins/source/k8s/resources/services/core/services.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@ func Services() *schema.Table {
3838
{
3939
Name: "spec_cluster_ips",
4040
Type: schema.TypeInetArray,
41-
Resolver: schema.PathResolver("Spec.ClusterIPs"),
41+
Resolver: client.StringToInetArrayPathResolver("Spec.ClusterIPs"),
4242
},
4343
{
4444
Name: "spec_external_ips",
4545
Type: schema.TypeInetArray,
46-
Resolver: schema.PathResolver("Spec.ExternalIPs"),
46+
Resolver: client.StringToInetArrayPathResolver("Spec.ExternalIPs"),
4747
},
4848
{
4949
Name: "spec_load_balancer_ip",

0 commit comments

Comments
 (0)