1

我正在关注@ymmt2005 优秀的动态客户指南。一切都很好,直到我进行实际 PATCH 调用的最后一步,我得到一个the server could not find the requested resource错误。几乎一切似乎都是正确的,除了我不确定 PathOptions 结构中的“FieldManager”字段。我不确定“进行这些更改的演员或实体”指的是什么。这是否需要匹配我的代码或系统中的某些内容?还有其他想法吗?

package main

import (
...
)

const resourceYAML = `
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mike-nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: 'nginx:latest'
          ports:
            - containerPort: 80
`

func main() {
    ctx := context.Background()

    // Create dynamic discovery client from local kubeconfig file
    kubePath := filepath.Join(homedir.HomeDir(), ".kube", "config")
    cfg, err := clientcmd.BuildConfigFromFlags("", kubePath)
    if err != nil {
        log.Fatalf("error building config, %v\n", err)
    }
    dynClient, err := dynamic.NewForConfig(cfg)
    if err != nil {
        log.Fatalf("error creating client, %v\n", err)
    }
    disClient, err := discovery.NewDiscoveryClientForConfig(cfg)
    if err != nil {
        log.Fatalf("error creating discovery client, %v\n", err)
    }

    // Decode YAML manifest & get GVK
    decodeUnstr := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
    obj := &unstructured.Unstructured{}
    _, gvk, err := decodeUnstr.Decode([]byte(resourceYAML), nil, obj)
    if err != nil {
        log.Fatalf("error decoding manifest, %v\n", err)
    }
    jsonObj, err := json.Marshal(obj)
    if err != nil {
        log.Fatalf("error marshaling object, %v\n", err)
    }

    // Find GVR using GVK
    mapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(disClient))
    mapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
    if err != nil {
        log.Fatalf("error finding GVR, %v\n", err)
    }

    // Get REST interface for the GVR, checking for namespace or cluster-wide
    var dr dynamic.ResourceInterface
    if mapping.Scope.Name() == meta.RESTScopeNameNamespace {
        // Namespaced resource
        dr = dynClient.Resource(mapping.Resource).Namespace(obj.GetNamespace())
    } else {
        // Cluster-wide resource
        dr = dynClient.Resource(mapping.Resource)
    }

    // Create or Update the object with SSA
    options := metav1.PatchOptions{FieldManager: "sample-controller"}
    _, err = dr.Patch(ctx, obj.GetName(), types.ApplyPatchType, jsonObj, options)
    if err != nil {
        log.Fatalf("error patching, %v\n", err)
    }
}

[编辑] 我确认我只能在已经存在的资源上使用“补丁”。我调整了代码以使用“创建”来创建资源,然后我能够成功地对其进行“补丁”以进行更改。为了克服 FieldManager 的不一致,我在文档中推荐的 PatchOptions 添加了 Force=true 。我仍然想知道如果资源不存在我如何创建并更新如果它存在 - 也许只是测试存在?

4

1 回答 1

1

答案真的很琐碎。原始代码假定namespace清单中提供了该代码。如果提供的命名空间是“”,部署端点不会自动将命名空间default设置为,并且会因为“”不是有效的命名空间而出错。因此,我添加了逻辑来将命名空间设置为default如果没有提供并且 presto,如果资源不存在,服务器端应用将创建资源,如果存在则更新。再次感谢@ymmt2005。

package main

import (
...
)

const resourceYAML = `
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mike-nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: 'nginx:latest'
          ports:
            - containerPort: 80
`

func main() {
    ctx := context.Background()

    // Create dynamic discovery client from local kubeconfig file
    kubePath := filepath.Join(homedir.HomeDir(), ".kube", "config")
    cfg, err := clientcmd.BuildConfigFromFlags("", kubePath)
    if err != nil {
        log.Fatalf("error building config, %v\n", err)
    }
    dynClient, err := dynamic.NewForConfig(cfg)
    if err != nil {
        log.Fatalf("error creating client, %v\n", err)
    }
    disClient, err := discovery.NewDiscoveryClientForConfig(cfg)
    if err != nil {
        log.Fatalf("error creating discovery client, %v\n", err)
    }

    // Decode YAML manifest & get GVK
    decodeUnstr := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
    obj := &unstructured.Unstructured{}
    _, gvk, err := decodeUnstr.Decode([]byte(resourceYAML), nil, obj)
    if err != nil {
        log.Fatalf("error decoding manifest, %v\n", err)
    }
    jsonObj, err := json.Marshal(obj)
    if err != nil {
        log.Fatalf("error marshaling object, %v\n", err)
    }

    // Find GVR using GVK
    mapper := restmapper.NewDeferredDiscoveryRESTMapper(memory.NewMemCacheClient(disClient))
    mapping, err := mapper.RESTMapping(gvk.GroupKind(), gvk.Version)
    if err != nil {
        log.Fatalf("error finding GVR, %v\n", err)
    }

    // Set Namespace to default if not provided in manifest
    var ns string
    if ns = obj.GetNamespace(); ns == "" {
        ns = "default"
    }

    // Get REST interface for the GVR, checking for namespace or cluster-wide
    var dr dynamic.ResourceInterface
    if mapping.Scope.Name() == meta.RESTScopeNameNamespace {
        // Namespaced resource
        dr = dynClient.Resource(mapping.Resource).Namespace(ns)
    } else {
        // Cluster-wide resource
        dr = dynClient.Resource(mapping.Resource)
    }

    // Create or Update the object with SSA
    options := metav1.PatchOptions{FieldManager: "sample-controller"}
    _, err = dr.Patch(ctx, obj.GetName(), types.ApplyPatchType, jsonObj, options)
    if err != nil {
        log.Fatalf("error patching, %v\n", err)
    }
}
于 2021-01-01T17:21:38.557 回答