Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Research for creating a readCoreV1NamespacedPodStatus conformant test #85782

Open
BobyMCbobs opened this issue Dec 2, 2019 · 1 comment

Comments

@BobyMCbobs
Copy link
Member

@BobyMCbobs BobyMCbobs commented Dec 2, 2019

Description

After looking through endpoints which need tests written for in APIsnoop, this appears to be an endpoint which isn't hit.

Fetch dependencies

go get -v -u k8s.io/apimachinery/pkg/apis/meta/v1
go get -v -u k8s.io/client-go/kubernetes
go get -v -u k8s.io/client-go/tools/clientcmd
go get -v -u github.com/ghodss/yaml

Test draft

package main

import (
  "fmt"
  "flag"
  "time"
  "os"
  "k8s.io/api/core/v1"
  metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  "k8s.io/client-go/kubernetes"                 
  "k8s.io/client-go/tools/clientcmd"
  "github.com/ghodss/yaml"
)

func main() {
  // uses the current context in kubeconfig
    kubeconfig := flag.String("kubeconfig",
      fmt.Sprintf("%v/%v/%v", os.Getenv("HOME"), ".kube", "config"),
      "(optional) absolute path to the kubeconfig file")
    flag.Parse()
    config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
    if err != nil {
      fmt.Println(err)
    }
  // make our work easier to find in the audit_event queries
  config.UserAgent = "live-test-writing"
  // creates the clientset
  clientset, _ := kubernetes.NewForConfig(config)
  // access the API to list pods

  _, err = clientset.CoreV1().Pods("default").Create(&v1.Pod{
          ObjectMeta: metav1.ObjectMeta{                               
                  Name: "nginx",                         
                  Labels: map[string]string{                           
                          "pod-name": "nginx",                         
                  },                                                   
          },                                                           
          Spec: v1.PodSpec{                                            
                  Containers: []v1.Container{{
                          Image: "nginx",
                          Name: "nginx",
                  }},                           
                  RestartPolicy: v1.RestartPolicyNever,                
          },                                                           
  })                                                                   

  if err != nil {
    fmt.Println(err)
    return
  }

  time.Sleep(5 * time.Second)

  pod, err := clientset.CoreV1().Pods("default").Get("nginx", metav1.GetOptions{})

  if err != nil {   
    fmt.Println(err)
    return          
  }                 
  podYAML, err := yaml.Marshal(pod)
  if err != nil {
    fmt.Printf("err: %v\n", err)
    return
  }
  fmt.Println(string(podYAML))

  time.Sleep(5 * time.Second)

  err = clientset.CoreV1().Pods("default").Delete("nginx", &metav1.DeleteOptions{})

  if err != nil {   
    fmt.Println(err)
    return          
  }       
}          

Test draft- Python working implementation

pip3 install kubernetes

from __future__ import print_function
import time
import kubernetes.client
from kubernetes.client.rest import ApiException
from pprint import pprint
from kubernetes.client.configuration import Configuration
from kubernetes.config import kube_config 
configuration = Configuration()
configuration.host = None
kube_config.load_kube_config(client_configuration=configuration)
# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
# configuration.api_key_prefix['authorization'] = 'Bearer'

# create an instance of the API class
api_instance = kubernetes.client.CoreV1Api(kubernetes.client.ApiClient(configuration))
name = 'kindnet-c7vtg' # str | name of the Pod
namespace = 'kube-system' # str | object name and auth scope, such as for teams and projects
pretty = 'pretty_example' # str | If 'true', then the output is pretty printed. (optional)

try:
    api_response = api_instance.read_namespaced_pod_status(name, namespace, pretty=pretty)
    pprint(api_response)
except ApiException as e:
    print("Exception when calling CoreV1Api->read_namespaced_pod_status: %s\n" % e)

Verify with APISnoop

create view for hit endpoints

CREATE VIEW "public"."endpoints_hit_by_new_test" AS
 WITH live_testing_endpoints AS (
   SELECT DISTINCT
     operation_id,
     count(1) as hits
     FROM
         audit_event
    WHERE bucket = 'apisnoop'
      AND useragent = 'live-test-writing'
      GROUP BY operation_id
   ), baseline AS  (
   SELECT DISTINCT
     operation_id,
     test_hits,
     conf_hits
       FROM endpoint_coverage where bucket != 'apisnoop' 
   )
 SELECT DISTINCT
   lte.operation_id,
   b.test_hits as hit_by_ete,
   lte.hits as hit_by_new_test
     FROM live_testing_endpoints lte
            JOIN baseline b ON (b.operation_id = lte.operation_id);

create view for coverage changed

CREATE OR REPLACE VIEW "public"."projected_change_in_coverage" AS
 with baseline as (
   SELECT *
     FROM
         stable_endpoint_stats
    WHERE job != 'live'
 ), test as (
   SELECT
     count(1) as endpoints_hit
     FROM
         (
           select
             operation_id
     FROM audit_event
      WHERE useragent = 'live-test-writing'
     EXCEPT 
     SELECT
       operation_id
     FROM
         endpoint_coverage
         WHERE test_hits > 0
               ) tested_endpoints
 ), coverage as (
   SELECT
   baseline.test_hits as old_coverage,
   (baseline.test_hits::int + test.endpoints_hit::int ) as new_coverage
   from baseline, test
 )
 select
   'test_coverage' as category,
   baseline.total_endpoints,
   coverage.old_coverage,
   coverage.new_coverage,
   (coverage.new_coverage - coverage.old_coverage) as change_in_number
   from baseline, coverage
          ;

find endpoints hit by this test

select * from endpoints_hit_by_new_test;
       operation_id        | hit_by_ete | hit_by_new_test 
---------------------------+------------+-----------------
 createCoreV1NamespacedPod |       1990 |              2 
 deleteCoreV1NamespacedPod |       2114 |              2 
 readCoreV1NamespacedPod   |      11421 |              1 
(3 rows)                                                  

show the change in coverage

select * from projected_change_in_coverage;
   category    | total_endpoints | old_coverage | new_coverage | change_in_number 
---------------+-----------------+--------------+--------------+------------------
 test_coverage |             430 |          167 |          167 |                0 
(1 row)                                                                           

Final notes

From the endpoints hit report above, it doesn't appear that my draft test hit the target endpoint.

Would it be possible for some help and/or advice on hitting the /api/v1/namespaces/NAMESPACE/pods/PODNAME/status endpoint?

@BobyMCbobs

This comment has been minimized.

Copy link
Member Author

@BobyMCbobs BobyMCbobs commented Dec 2, 2019

/sig testing
/sig architecture
/area conformance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.