-
Notifications
You must be signed in to change notification settings - Fork 543
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a round-tripper to ensure we label non-OLM resources
This round-tripper is added to our *rest.Config when it's possible to detect that we're in a CI environment. Developers should set $CI=true to get this behavior locally. Signed-off-by: Steve Kuznetsov <[email protected]>
- Loading branch information
1 parent
2976198
commit dd247bd
Showing
3 changed files
with
64 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
pkg/controller/operators/validatingroundtripper/validating_round_tripper.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package validatingroundtripper | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/operator-framework/operator-lifecycle-manager/pkg/controller/install" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/util/yaml" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
type validatingRoundTripper struct { | ||
delegate http.RoundTripper | ||
} | ||
|
||
func (rt *validatingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) { | ||
if req.Method == "POST" { | ||
b, err := req.GetBody() | ||
if err != nil { | ||
panic(err) | ||
} | ||
dec := yaml.NewYAMLOrJSONDecoder(b, 10) | ||
unstructuredObject := &unstructured.Unstructured{} | ||
if err := dec.Decode(unstructuredObject); err != nil { | ||
panic(fmt.Errorf("error decoding object to an unstructured object: %w", err)) | ||
} | ||
gvk := unstructuredObject.GroupVersionKind() | ||
if gvk.Kind != "Event" { | ||
if labels := unstructuredObject.GetLabels(); labels[install.OLMManagedLabelKey] != install.OLMManagedLabelValue { | ||
panic(fmt.Errorf("%s.%s/%v %s/%s does not have labels[%s]=%s", gvk.Kind, gvk.Group, gvk.Version, unstructuredObject.GetNamespace(), unstructuredObject.GetName(), install.OLMManagedLabelKey, install.OLMManagedLabelValue)) | ||
} | ||
} | ||
} | ||
return rt.delegate.RoundTrip(req) | ||
} | ||
|
||
var _ http.RoundTripper = (*validatingRoundTripper)(nil) | ||
|
||
// Wrap is meant to be used in developer environments and CI to make it easy to find places | ||
// where we accidentally create Kubernetes objects without our management label. | ||
func Wrap(cfg *rest.Config) *rest.Config { | ||
if _, set := os.LookupEnv("CI"); !set { | ||
return cfg | ||
} | ||
|
||
cfgCopy := *cfg | ||
cfgCopy.Wrap(func(rt http.RoundTripper) http.RoundTripper { | ||
return &validatingRoundTripper{delegate: rt} | ||
}) | ||
return &cfgCopy | ||
} |