-
-
Save ahmetb/50784526a54244d45022a7ed8d556a62 to your computer and use it in GitHub Desktop.
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
module foo | |
go 1.21.1 | |
require ( | |
k8s.io/api v0.28.1 | |
k8s.io/client-go v0.28.1 | |
sigs.k8s.io/controller-runtime v0.16.1 | |
) | |
require ( | |
github.com/davecgh/go-spew v1.1.1 // indirect | |
github.com/emicklei/go-restful/v3 v3.9.0 // indirect | |
github.com/evanphx/json-patch v5.6.0+incompatible // indirect | |
github.com/evanphx/json-patch/v5 v5.6.0 // indirect | |
github.com/go-logr/logr v1.2.4 // indirect | |
github.com/go-openapi/jsonpointer v0.19.6 // indirect | |
github.com/go-openapi/jsonreference v0.20.2 // indirect | |
github.com/go-openapi/swag v0.22.3 // indirect | |
github.com/gogo/protobuf v1.3.2 // indirect | |
github.com/golang/protobuf v1.5.3 // indirect | |
github.com/google/gnostic-models v0.6.8 // indirect | |
github.com/google/go-cmp v0.5.9 // indirect | |
github.com/google/gofuzz v1.2.0 // indirect | |
github.com/google/uuid v1.3.0 // indirect | |
github.com/josharian/intern v1.0.0 // indirect | |
github.com/json-iterator/go v1.1.12 // indirect | |
github.com/mailru/easyjson v0.7.7 // indirect | |
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | |
github.com/modern-go/reflect2 v1.0.2 // indirect | |
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect | |
github.com/pkg/errors v0.9.1 // indirect | |
golang.org/x/net v0.13.0 // indirect | |
golang.org/x/oauth2 v0.8.0 // indirect | |
golang.org/x/sys v0.11.0 // indirect | |
golang.org/x/term v0.10.0 // indirect | |
golang.org/x/text v0.11.0 // indirect | |
golang.org/x/time v0.3.0 // indirect | |
google.golang.org/appengine v1.6.7 // indirect | |
google.golang.org/protobuf v1.30.0 // indirect | |
gopkg.in/inf.v0 v0.9.1 // indirect | |
gopkg.in/yaml.v2 v2.4.0 // indirect | |
gopkg.in/yaml.v3 v3.0.1 // indirect | |
k8s.io/apimachinery v0.28.1 // indirect | |
k8s.io/klog/v2 v2.100.1 // indirect | |
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect | |
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect | |
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect | |
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect | |
sigs.k8s.io/yaml v1.3.0 // indirect | |
) |
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
package main | |
import ( | |
"context" | |
"fmt" | |
corev1 "k8s.io/api/core/v1" | |
"k8s.io/client-go/kubernetes/scheme" | |
"sigs.k8s.io/controller-runtime/pkg/client" | |
"sigs.k8s.io/controller-runtime/pkg/client/fake" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
) | |
func main() { | |
// Create a new fake client. | |
scheme := scheme.Scheme | |
cl := fake.NewClientBuilder().WithScheme(scheme).Build() | |
// Create a Pod object. | |
pod := &corev1.Pod{ | |
ObjectMeta: metav1.ObjectMeta{ | |
Name: "example-pod", | |
Namespace: "default", | |
}, | |
Spec: corev1.PodSpec{ | |
Containers: []corev1.Container{ | |
{ | |
Name: "nginx", | |
Image: "nginx:latest", | |
}, | |
}, | |
}, | |
} | |
// Create the Pod on the fake client. | |
err := cl.Create(context.Background(), pod) | |
if err != nil { | |
fmt.Printf("Error creating Pod: %v\n", err) | |
return | |
} | |
// Simulate some changes to the Pod's status. | |
pod.Status.Phase = corev1.PodRunning | |
// Update the Pod on the fake client. | |
// !!!!! not using cl.Status().Update !!!! | |
err = cl.Update(context.Background(), pod) | |
if err != nil { | |
fmt.Printf("Error updating Pod: %v\n", err) | |
return | |
} | |
// Get the Pod back from the fake client. | |
updatedPod := &corev1.Pod{} | |
err = cl.Get(context.Background(), client.ObjectKey{Namespace: "default", Name: "example-pod"}, updatedPod) | |
if err != nil { | |
fmt.Printf("Error getting Pod: %v\n", err) | |
return | |
} | |
// Print the updated Pod's status. | |
fmt.Printf("Updated Pod Status: %v\n", updatedPod.Status) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment