1

I'm trying to list knative (v0.17.0) services, I have a clientset but I don't know where to start. Here is a service I launched for my test :

apiVersion: serving.knative.dev/v1alpha1
kind: Service
metadata:
  name: "helloworld"
spec:
  runLatest:
    configuration:
      revisionTemplate:
        spec:
          container:
            image: "gcr.io/knative-samples/helloworld-go"
            env:
              - name: "TARGET"
                value: "world"

If you have any advice, tutorial or example it would be great

Quentin
  • 327
  • 3
  • 10

2 Answers2

2

Basing this off this gist adapted for the post-1.18 client-go, and the Serving clientset godoc:

import (
  "context"
  "fmt"
  metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  "k8s.io/client-go/tools/clientcmd"
  servingv1 "knative.dev/serving/pkg/client/clientset/versioned/typed/serving/v1"
}

func doIt() error {
  config, err := clientcmd.BuildConfigFromFLags("", "") // Uses defaults
  if err != nil {
    return err
  }

  serving, err := servingv1.NewForConfig(config)
  if err != nil {
    return err
  }

  // Get services in the default namespace
  list, err := serving.Services("default").List(context.Background(), metav1.ListOptions{})
  if err != nil {
    return err
  }
  // How to print them out.
  fmt.Printf("There are %d services in the default namespace", len(list.Items))
  for _, i := range list.Items {
    fmt.Printf("  > Service %q", i.Name)
  }
}
E. Anderson
  • 3,405
  • 1
  • 16
  • 19
  • Thanks it's clearly the starting point I needed. BTW it's `knative.dev/serving/pkg/` not `knative.dev/servig/pkg/` ;) – Quentin Feb 15 '21 at 22:03
  • Oh and nothing really related but do you have matrix of every packages related to kubernetes. I struggle a lot to find the right version of each package. Or a tools which could scaffold the `go.mod` – Quentin Feb 15 '21 at 22:04
  • (Feel free to accept the answer if it worked for you) – E. Anderson Apr 01 '21 at 19:06
  • In terms of core Kubernetes packages, you should be able to use https://pkg.go.dev/k8s.io/client-go – E. Anderson Apr 01 '21 at 19:07
  • Yes sorry, I didn't know how to. Thanks for your answer it helped me a lot – Quentin Apr 02 '21 at 20:36
0

If you wanna list knative services then you wanna use the API here.

For getting you configurations from this yaml, you would set them as Environment Variables in env list and then in your code call:

os.GetEnv("TARGET")

Now using your key from env and the API you can list services.

jimist
  • 19
  • 3