本文整理匯總了Golang中k8s/io/kubernetes/pkg/client/unversioned.Client.Patch方法的典型用法代碼示例。如果您正苦於以下問題:Golang Client.Patch方法的具體用法?Golang Client.Patch怎麽用?Golang Client.Patch使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類k8s/io/kubernetes/pkg/client/unversioned.Client
的用法示例。
在下文中一共展示了Client.Patch方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Update
// Update updates an existing node api object with new labels
func Update(client *client.Client, n *api.Node, labels map[string]string) (*api.Node, error) {
patch := struct {
Metadata struct {
Labels map[string]string `json:"labels"`
} `json:"metadata"`
}{}
patch.Metadata.Labels = map[string]string{}
for k, v := range n.Labels {
if !IsSlaveAttributeLabel(k) {
patch.Metadata.Labels[k] = v
}
}
for k, v := range labels {
patch.Metadata.Labels[k] = v
}
patchJson, _ := json.Marshal(patch)
log.V(4).Infof("Patching labels of node %q: %v", n.Name, string(patchJson))
err := client.Patch(api.MergePatchType).RequestURI(n.SelfLink).Body(patchJson).Do().Error()
if err != nil {
return nil, fmt.Errorf("error updating labels of node %q: %v", n.Name, err)
}
newNode, err := api.Scheme.DeepCopy(n)
if err != nil {
return nil, err
}
newNode.(*api.Node).Labels = patch.Metadata.Labels
return newNode.(*api.Node), nil
}
示例2: runPatchTest
func runPatchTest(c *client.Client) {
name := "patchservice"
resource := "services"
svcBody := api.Service{
TypeMeta: api.TypeMeta{
APIVersion: c.APIVersion(),
},
ObjectMeta: api.ObjectMeta{
Name: name,
Labels: map[string]string{},
},
Spec: api.ServiceSpec{
// This is here because validation requires it.
Selector: map[string]string{
"foo": "bar",
},
Ports: []api.ServicePort{{
Port: 12345,
Protocol: "TCP",
}},
SessionAffinity: "None",
},
}
services := c.Services(api.NamespaceDefault)
svc, err := services.Create(&svcBody)
if err != nil {
glog.Fatalf("Failed creating patchservice: %v", err)
}
patchBodies := map[string]map[api.PatchType]struct {
AddLabelBody []byte
RemoveLabelBody []byte
RemoveAllLabelsBody []byte
}{
"v1": {
api.JSONPatchType: {
[]byte(`[{"op":"add","path":"/metadata/labels","value":{"foo":"bar","baz":"qux"}}]`),
[]byte(`[{"op":"remove","path":"/metadata/labels/foo"}]`),
[]byte(`[{"op":"remove","path":"/metadata/labels"}]`),
},
api.MergePatchType: {
[]byte(`{"metadata":{"labels":{"foo":"bar","baz":"qux"}}}`),
[]byte(`{"metadata":{"labels":{"foo":null}}}`),
[]byte(`{"metadata":{"labels":null}}`),
},
api.StrategicMergePatchType: {
[]byte(`{"metadata":{"labels":{"foo":"bar","baz":"qux"}}}`),
[]byte(`{"metadata":{"labels":{"foo":null}}}`),
[]byte(`{"metadata":{"labels":{"$patch":"replace"}}}`),
},
},
}
pb := patchBodies[c.APIVersion()]
execPatch := func(pt api.PatchType, body []byte) error {
return c.Patch(pt).
Resource(resource).
Namespace(api.NamespaceDefault).
Name(name).
Body(body).
Do().
Error()
}
for k, v := range pb {
// add label
err := execPatch(k, v.AddLabelBody)
if err != nil {
glog.Fatalf("Failed updating patchservice with patch type %s: %v", k, err)
}
svc, err = services.Get(name)
if err != nil {
glog.Fatalf("Failed getting patchservice: %v", err)
}
if len(svc.Labels) != 2 || svc.Labels["foo"] != "bar" || svc.Labels["baz"] != "qux" {
glog.Fatalf("Failed updating patchservice with patch type %s: labels are: %v", k, svc.Labels)
}
// remove one label
err = execPatch(k, v.RemoveLabelBody)
if err != nil {
glog.Fatalf("Failed updating patchservice with patch type %s: %v", k, err)
}
svc, err = services.Get(name)
if err != nil {
glog.Fatalf("Failed getting patchservice: %v", err)
}
if len(svc.Labels) != 1 || svc.Labels["baz"] != "qux" {
glog.Fatalf("Failed updating patchservice with patch type %s: labels are: %v", k, svc.Labels)
}
// remove all labels
err = execPatch(k, v.RemoveAllLabelsBody)
if err != nil {
glog.Fatalf("Failed updating patchservice with patch type %s: %v", k, err)
}
svc, err = services.Get(name)
if err != nil {
glog.Fatalf("Failed getting patchservice: %v", err)
}
//.........這裏部分代碼省略.........
示例3:
},
})
expectNoError(err)
expectNoError(waitForPodRunningInNamespace(c, podName, ns))
pod, err := c.Pods(ns).Get(podName)
expectNoError(err)
nodeName := pod.Spec.NodeName
err = c.Pods(ns).Delete(podName, api.NewDeleteOptions(0))
expectNoError(err)
By("Trying to apply a random label on the found node.")
k := fmt.Sprintf("kubernetes.io/e2e-%s", string(util.NewUUID()))
v := "42"
patch := fmt.Sprintf(`{"metadata":{"labels":{"%s":"%s"}}}`, k, v)
err = c.Patch(api.MergePatchType).Resource("nodes").Name(nodeName).Body([]byte(patch)).Do().Error()
expectNoError(err)
node, err := c.Nodes().Get(nodeName)
expectNoError(err)
Expect(node.Labels[k]).To(Equal(v))
By("Trying to relaunch the pod, now with labels.")
labelPodName := "with-labels"
_, err = c.Pods(ns).Create(&api.Pod{
TypeMeta: unversioned.TypeMeta{
Kind: "Pod",
},
ObjectMeta: api.ObjectMeta{
Name: labelPodName,
},
示例4: runPatchTest
func runPatchTest(c *client.Client) {
name := "patchservice"
resource := "services"
svcBody := api.Service{
TypeMeta: unversioned.TypeMeta{
APIVersion: c.APIVersion().String(),
},
ObjectMeta: api.ObjectMeta{
Name: name,
Labels: map[string]string{},
},
Spec: api.ServiceSpec{
// This is here because validation requires it.
Selector: map[string]string{
"foo": "bar",
},
Ports: []api.ServicePort{{
Port: 12345,
Protocol: "TCP",
}},
SessionAffinity: "None",
},
}
services := c.Services(api.NamespaceDefault)
svc, err := services.Create(&svcBody)
if err != nil {
glog.Fatalf("Failed creating patchservice: %v", err)
}
patchBodies := map[unversioned.GroupVersion]map[api.PatchType]struct {
AddLabelBody []byte
RemoveLabelBody []byte
RemoveAllLabelsBody []byte
}{
v1.SchemeGroupVersion: {
api.JSONPatchType: {
[]byte(`[{"op":"add","path":"/metadata/labels","value":{"foo":"bar","baz":"qux"}}]`),
[]byte(`[{"op":"remove","path":"/metadata/labels/foo"}]`),
[]byte(`[{"op":"remove","path":"/metadata/labels"}]`),
},
api.MergePatchType: {
[]byte(`{"metadata":{"labels":{"foo":"bar","baz":"qux"}}}`),
[]byte(`{"metadata":{"labels":{"foo":null}}}`),
[]byte(`{"metadata":{"labels":null}}`),
},
api.StrategicMergePatchType: {
[]byte(`{"metadata":{"labels":{"foo":"bar","baz":"qux"}}}`),
[]byte(`{"metadata":{"labels":{"foo":null}}}`),
[]byte(`{"metadata":{"labels":{"$patch":"replace"}}}`),
},
},
}
pb := patchBodies[c.APIVersion()]
execPatch := func(pt api.PatchType, body []byte) error {
return c.Patch(pt).
Resource(resource).
Namespace(api.NamespaceDefault).
Name(name).
Body(body).
Do().
Error()
}
for k, v := range pb {
// add label
err := execPatch(k, v.AddLabelBody)
if err != nil {
glog.Fatalf("Failed updating patchservice with patch type %s: %v", k, err)
}
svc, err = services.Get(name)
if err != nil {
glog.Fatalf("Failed getting patchservice: %v", err)
}
if len(svc.Labels) != 2 || svc.Labels["foo"] != "bar" || svc.Labels["baz"] != "qux" {
glog.Fatalf("Failed updating patchservice with patch type %s: labels are: %v", k, svc.Labels)
}
// remove one label
err = execPatch(k, v.RemoveLabelBody)
if err != nil {
glog.Fatalf("Failed updating patchservice with patch type %s: %v", k, err)
}
svc, err = services.Get(name)
if err != nil {
glog.Fatalf("Failed getting patchservice: %v", err)
}
if len(svc.Labels) != 1 || svc.Labels["baz"] != "qux" {
glog.Fatalf("Failed updating patchservice with patch type %s: labels are: %v", k, svc.Labels)
}
// remove all labels
err = execPatch(k, v.RemoveAllLabelsBody)
if err != nil {
glog.Fatalf("Failed updating patchservice with patch type %s: %v", k, err)
}
svc, err = services.Get(name)
if err != nil {
glog.Fatalf("Failed getting patchservice: %v", err)
}
//.........這裏部分代碼省略.........
示例5: verifyEvents
return verifyEvents(c.Events(eventNamespace), eventListOptions, num, tempReason, tempMessage)
}, pollConsistent, pollInterval).Should(Succeed())
})
AfterEach(func() {
By("Delete the node problem detector")
c.Pods(ns).Delete(name, api.NewDeleteOptions(0))
By("Wait for the node problem detector to disappear")
Expect(framework.WaitForPodToDisappear(c, ns, name, labels.Everything(), pollInterval, pollTimeout)).To(Succeed())
By("Delete the config map")
c.ConfigMaps(ns).Delete(configName)
By("Clean up the events")
Expect(c.Events(eventNamespace).DeleteCollection(api.NewDeleteOptions(0), eventListOptions)).To(Succeed())
By("Clean up the node condition")
patch := []byte(fmt.Sprintf(`{"status":{"conditions":[{"$patch":"delete","type":"%s"}]}}`, condition))
c.Patch(api.StrategicMergePatchType).Resource("nodes").Name(node.Name).SubResource("status").Body(patch).Do()
By("Clean up the temporary directory")
framework.IssueSSHCommand(fmt.Sprintf("rm -r %s", tmpDir), framework.TestContext.Provider, node)
})
})
})
// verifyEvents verifies there are num specific events generated
func verifyEvents(e client.EventInterface, options api.ListOptions, num int, reason, message string) error {
events, err := e.List(options)
if err != nil {
return err
}
count := 0
for _, event := range events.Items {
if event.Reason != reason || event.Message != message {