Skip to content

Instantly share code, notes, and snippets.

@yitsushi
Created July 4, 2023 08:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yitsushi/f5fb99b1c70afc93faefb6ef154c6492 to your computer and use it in GitHub Desktop.
Save yitsushi/f5fb99b1c70afc93faefb6ef154c6492 to your computer and use it in GitHub Desktop.
It uses the specified GVK, but then ignores it
---
apiVersion: source.toolkit.fluxcd.io/v1beta2
kind: GitRepository
metadata:
name: hello-world
namespace: hello-world
spec:
interval: 10m0s
ref:
branch: main
url: https://github.com/yitsushi/hello-world
package main
import (
"context"
"log"
sourcev1 "github.com/fluxcd/source-controller/api/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/config"
)
func main() {
c, err := client.New(config.GetConfigOrDie(), client.Options{})
if err != nil {
log.Printf(" !! error: %s\n", err)
return
}
key := client.ObjectKey{Namespace: "hello-world", Name: "hello-world"}
log.Printf(" ++ gitrepo v1\n")
resp := getResource(c, key, schema.GroupVersionKind{
Group: "source.toolkit.fluxcd.io",
Kind: "GitRepository",
Version: "v1",
})
logGitRepository(resp)
log.Printf("\n\n ++ gitrepo v1beta2\n")
resp = getResource(c, key, schema.GroupVersionKind{
Group: "source.toolkit.fluxcd.io",
Kind: "GitRepository",
Version: "v1beta2",
})
logGitRepository(resp)
log.Printf("\n\n ++ gitrepo v99omega18\n")
resp = getResource(c, key, schema.GroupVersionKind{
Group: "source.toolkit.fluxcd.io",
Kind: "GitRepository",
Version: "v99omega18",
})
logGitRepository(resp)
}
func logGitRepository(obj *unstructured.Unstructured) {
var repo sourcev1.GitRepository
err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), &repo)
if err != nil {
log.Printf(" !! error: %s\n", err)
}
log.Printf(" -- unstructured gvk: %+v\n", obj.GroupVersionKind())
log.Printf(" -- repo gvk: %+v\n", repo.GroupVersionKind())
log.Printf(" -- repo uid: %v\n", repo.GetUID())
//log.Printf(" -- repo gvk: %+v\n", repo)
}
func getResource(c client.Client, objKey client.ObjectKey, gvk schema.GroupVersionKind) *unstructured.Unstructured {
u := &unstructured.Unstructured{}
u.SetGroupVersionKind(gvk)
err := c.Get(context.Background(), objKey, u)
if err != nil {
log.Printf(" !! error: %s\n", err)
}
return u
}
2023/07/04 10:16:49 ++ gitrepo v1
2023/07/04 10:16:49 -- unstructured gvk: source.toolkit.fluxcd.io/v1, Kind=GitRepository
2023/07/04 10:16:49 -- repo gvk: source.toolkit.fluxcd.io/v1, Kind=GitRepository
2023/07/04 10:16:49 -- repo uid: 019dc27b-3bc2-4fb4-aa59-971411b4f2ab
2023/07/04 10:16:49
++ gitrepo v1beta2
2023/07/04 10:16:49 -- unstructured gvk: source.toolkit.fluxcd.io/v1beta2, Kind=GitRepository
2023/07/04 10:16:49 -- repo gvk: source.toolkit.fluxcd.io/v1beta2, Kind=GitRepository
2023/07/04 10:16:49 -- repo uid: 019dc27b-3bc2-4fb4-aa59-971411b4f2ab
2023/07/04 10:16:49
++ gitrepo v99omega18
2023/07/04 10:16:49 !! error: failed to get API group resources: unable to retrieve the complete list of server APIs: source.toolkit.fluxcd.io/v99omega18: the server could not find the requested resource
2023/07/04 10:16:49 -- unstructured gvk: source.toolkit.fluxcd.io/v99omega18, Kind=GitRepository
2023/07/04 10:16:49 -- repo gvk: source.toolkit.fluxcd.io/v99omega18, Kind=GitRepository
2023/07/04 10:16:49 -- repo uid:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment