clusterlint/kube/object_filter.go

53 lines
1.7 KiB
Go
Raw Normal View History

2020-01-09 11:47:38 +00:00
/*
Copyright 2022 DigitalOcean
2020-01-09 11:47:38 +00:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package kube
import (
"fmt"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
2020-01-09 11:47:38 +00:00
)
2020-01-20 05:34:13 +00:00
// ObjectFilter stores k8s object's fields that needs to be included or excluded while running checks
type ObjectFilter struct {
IncludeNamespace string
ExcludeNamespace string
2020-01-09 11:47:38 +00:00
}
2020-01-20 05:34:13 +00:00
// NewObjectFilter is a constructor to initialize an instance of ObjectFilter
func NewObjectFilter(includeNamespace, excludeNamespace string) (ObjectFilter, error) {
if len(includeNamespace) > 0 && len(excludeNamespace) > 0 {
2020-01-20 05:34:13 +00:00
return ObjectFilter{}, fmt.Errorf("cannot specify both include and exclude namespace conditions")
2020-01-09 11:47:38 +00:00
}
2020-01-20 05:34:13 +00:00
return ObjectFilter{
IncludeNamespace: includeNamespace,
ExcludeNamespace: excludeNamespace,
2020-01-09 11:47:38 +00:00
}, nil
}
// NamespaceOptions returns ListOptions for filtering by namespace
func (f ObjectFilter) NamespaceOptions(opts metav1.ListOptions) metav1.ListOptions {
if len(f.IncludeNamespace) > 0 {
opts.FieldSelector = fields.OneTermEqualSelector("metadata.namespace", f.IncludeNamespace).String()
2020-01-09 11:47:38 +00:00
}
if len(f.ExcludeNamespace) > 0 {
opts.FieldSelector = fields.OneTermNotEqualSelector("metadata.namespace", f.ExcludeNamespace).String()
2020-01-09 11:47:38 +00:00
}
return opts
}