Skip to content

Commit

Permalink
Add generated CRDs and update to K8s 1.18
Browse files Browse the repository at this point in the history
* Regenerates CRDs and adds Policy type
* Updates client-go to K8s 1.18
* Updates files for new changes using clientgofix which adds
context.TODO() where required. At a later date this needs to
be changed to any context we happen to be using in the
function.

https://github.com/kubernetes-sigs/clientgofix

Thanks to @aledbf for changes proposed in #632

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Jun 17, 2020
1 parent eb8485b commit 02266c7
Show file tree
Hide file tree
Showing 38 changed files with 1,238 additions and 194 deletions.
98 changes: 98 additions & 0 deletions artifacts/crds/openfaas.com_functions.yaml
@@ -0,0 +1,98 @@
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.3.0
creationTimestamp: null
name: functions.openfaas.com
spec:
group: openfaas.com
names:
kind: Function
listKind: FunctionList
plural: functions
singular: function
scope: Namespaced
validation:
openAPIV3Schema:
description: Function describes an OpenFaaS function
type: object
required:
- spec
properties:
apiVersion:
description: 'APIVersion defines the versioned schema of this representation
of an object. Servers should convert recognized schemas to the latest
internal value, and may reject unrecognized values. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources'
type: string
kind:
description: 'Kind is a string value representing the REST resource this
object represents. Servers may infer this from the endpoint the client
submits requests to. Cannot be updated. In CamelCase. More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds'
type: string
metadata:
type: object
spec:
description: FunctionSpec is the spec for a Function resource
type: object
required:
- image
- name
properties:
annotations:
type: object
additionalProperties:
type: string
constraints:
type: array
items:
type: string
environment:
type: object
additionalProperties:
type: string
handler:
type: string
image:
type: string
labels:
type: object
additionalProperties:
type: string
limits:
description: FunctionResources is used to set CPU and memory limits
and requests
type: object
properties:
cpu:
type: string
memory:
type: string
name:
type: string
readOnlyRootFilesystem:
type: boolean
requests:
description: FunctionResources is used to set CPU and memory limits
and requests
type: object
properties:
cpu:
type: string
memory:
type: string
secrets:
type: array
items:
type: string
version: v1
versions:
- name: v1
served: true
storage: true
status:
acceptedNames:
kind: ""
plural: ""
conditions: []
storedVersions: []
805 changes: 805 additions & 0 deletions artifacts/crds/openfaas.com_policies.yaml

Large diffs are not rendered by default.

13 changes: 6 additions & 7 deletions go.mod
Expand Up @@ -9,19 +9,18 @@ require (
github.com/gorilla/context v1.1.1 // indirect
github.com/gorilla/mux v1.6.2
github.com/imdario/mergo v0.3.7 // indirect
github.com/onsi/ginkgo v1.11.0 // indirect
github.com/onsi/gomega v1.8.1 // indirect
github.com/openfaas/faas v0.0.0-20191125105239-365f459b3f3a
github.com/openfaas/faas-provider v0.15.1
github.com/pkg/errors v0.8.1
github.com/prometheus/client_golang v0.9.2
github.com/prometheus/client_golang v1.0.0
github.com/prometheus/client_model v0.2.0 // indirect
go.uber.org/goleak v1.0.0 // indirect
golang.org/x/sys v0.0.0-20190920190810-ef0ce1748380 // indirect
google.golang.org/appengine v1.6.2 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
k8s.io/api v0.17.4
k8s.io/apimachinery v0.17.4
k8s.io/client-go v0.17.4
k8s.io/code-generator v0.17.4
k8s.io/api v0.18.2
k8s.io/apimachinery v0.18.2
k8s.io/client-go v0.18.2
k8s.io/code-generator v0.18.2
k8s.io/klog v1.0.0
)
115 changes: 60 additions & 55 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion hack/update-codegen.sh
Expand Up @@ -37,4 +37,4 @@ ${CODEGEN_PKG}/generate-groups.sh all \
--go-header-file ${SCRIPT_ROOT}/hack/boilerplate.go.txt

# Copy everything back.
cp -r "${TEMP_DIR}/github.com/openfaas/faas-netes/." "${SCRIPT_ROOT}/"
cp -r "${TEMP_DIR}/github.com/openfaas/faas-netes/." "${SCRIPT_ROOT}/"
16 changes: 16 additions & 0 deletions hack/update-crds.sh
@@ -0,0 +1,16 @@
#!/bin/bash

export controllergen="$GOPATH/bin/controller-gen"
export PKG=sigs.k8s.io/controller-tools/cmd/controller-gen

if [ ! -e "$gen" ]
then
echo "Getting $PKG"
go get $PKG
fi

"$controllergen" \
crd \
schemapatch:manifests=./artifacts/crds \
paths=./pkg/apis/... \
output:dir=./artifacts/crds
7 changes: 4 additions & 3 deletions handlers/delete.go
Expand Up @@ -4,6 +4,7 @@
package handlers

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -54,7 +55,7 @@ func MakeDeleteHandler(defaultNamespace string, clientset *kubernetes.Clientset)
// This makes sure we don't delete non-labelled deployments
deployment, findDeployErr := clientset.AppsV1().
Deployments(lookupNamespace).
Get(request.FunctionName, getOpts)
Get(context.TODO(), request.FunctionName, getOpts)

if findDeployErr != nil {
if errors.IsNotFound(findDeployErr) {
Expand Down Expand Up @@ -98,7 +99,7 @@ func deleteFunction(functionNamespace string, clientset *kubernetes.Clientset, r
opts := &metav1.DeleteOptions{PropagationPolicy: &foregroundPolicy}

if deployErr := clientset.AppsV1().Deployments(functionNamespace).
Delete(request.FunctionName, opts); deployErr != nil {
Delete(context.TODO(), request.FunctionName, *opts); deployErr != nil {

if errors.IsNotFound(deployErr) {
w.WriteHeader(http.StatusNotFound)
Expand All @@ -111,7 +112,7 @@ func deleteFunction(functionNamespace string, clientset *kubernetes.Clientset, r

if svcErr := clientset.CoreV1().
Services(functionNamespace).
Delete(request.FunctionName, opts); svcErr != nil {
Delete(context.TODO(), request.FunctionName, *opts); svcErr != nil {

if errors.IsNotFound(svcErr) {
w.WriteHeader(http.StatusNotFound)
Expand Down
5 changes: 3 additions & 2 deletions handlers/deploy.go
Expand Up @@ -4,6 +4,7 @@
package handlers

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -76,7 +77,7 @@ func MakeDeployHandler(functionNamespace string, factory k8s.FunctionFactory) ht

deploy := factory.Client.AppsV1().Deployments(namespace)

_, err = deploy.Create(deploymentSpec)
_, err = deploy.Create(context.TODO(), deploymentSpec, metav1.CreateOptions{})
if err != nil {
wrappedErr := fmt.Errorf("unable create Deployment: %s", err.Error())
log.Println(wrappedErr)
Expand All @@ -88,7 +89,7 @@ func MakeDeployHandler(functionNamespace string, factory k8s.FunctionFactory) ht

service := factory.Client.CoreV1().Services(namespace)
serviceSpec := makeServiceSpec(request, factory)
_, err = service.Create(serviceSpec)
_, err = service.Create(context.TODO(), serviceSpec, metav1.CreateOptions{})

if err != nil {
wrappedErr := fmt.Errorf("failed create Service: %s", err.Error())
Expand Down
3 changes: 2 additions & 1 deletion handlers/namespaces.go
Expand Up @@ -6,6 +6,7 @@ package handlers

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -74,7 +75,7 @@ func NewNamespaceResolver(defaultNamespace string, kube kubernetes.Interface) Na

func list(defaultNamespace string, clientset kubernetes.Interface) []string {
listOptions := metav1.ListOptions{}
namespaces, err := clientset.CoreV1().Namespaces().List(listOptions)
namespaces, err := clientset.CoreV1().Namespaces().List(context.TODO(), listOptions)

set := []string{}

Expand Down
5 changes: 3 additions & 2 deletions handlers/reader.go
Expand Up @@ -4,6 +4,7 @@
package handlers

import (
"context"
"encoding/json"
"fmt"
"log"
Expand Down Expand Up @@ -57,7 +58,7 @@ func getServiceList(functionNamespace string, clientset *kubernetes.Clientset) (
LabelSelector: "faas_function",
}

res, err := clientset.AppsV1().Deployments(functionNamespace).List(listOpts)
res, err := clientset.AppsV1().Deployments(functionNamespace).List(context.TODO(), listOpts)

if err != nil {
return nil, err
Expand All @@ -77,7 +78,7 @@ func getService(functionNamespace string, functionName string, clientset *kubern

getOpts := metav1.GetOptions{}

item, err := clientset.AppsV1().Deployments(functionNamespace).Get(functionName, getOpts)
item, err := clientset.AppsV1().Deployments(functionNamespace).Get(context.TODO(), functionName, getOpts)

if err != nil {
if errors.IsNotFound(err) {
Expand Down
5 changes: 3 additions & 2 deletions handlers/replicas.go
Expand Up @@ -4,6 +4,7 @@
package handlers

import (
"context"
"encoding/json"
"io/ioutil"
"log"
Expand Down Expand Up @@ -58,7 +59,7 @@ func MakeReplicaUpdater(defaultNamespace string, clientset *kubernetes.Clientset
},
}

deployment, err := clientset.AppsV1().Deployments(lookupNamespace).Get(functionName, options)
deployment, err := clientset.AppsV1().Deployments(lookupNamespace).Get(context.TODO(), functionName, options)

if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand All @@ -74,7 +75,7 @@ func MakeReplicaUpdater(defaultNamespace string, clientset *kubernetes.Clientset

deployment.Spec.Replicas = &replicas

_, err = clientset.AppsV1().Deployments(lookupNamespace).Update(deployment)
_, err = clientset.AppsV1().Deployments(lookupNamespace).Update(context.TODO(), deployment, metav1.UpdateOptions{})

if err != nil {
w.WriteHeader(http.StatusInternalServerError)
Expand Down
9 changes: 5 additions & 4 deletions handlers/secrets_test.go
Expand Up @@ -4,6 +4,7 @@
package handlers

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -40,7 +41,7 @@ func Test_SecretsHandler(t *testing.T) {
t.Errorf("want status code '%d', got '%d'", http.StatusAccepted, resp.StatusCode)
}

actualSecret, err := kube.CoreV1().Secrets(namespace).Get("testsecret", metav1.GetOptions{})
actualSecret, err := kube.CoreV1().Secrets(namespace).Get(context.TODO(), "testsecret", metav1.GetOptions{})
if err != nil {
t.Errorf("error validting secret: %s", err)
}
Expand Down Expand Up @@ -82,7 +83,7 @@ func Test_SecretsHandler(t *testing.T) {
t.Errorf("want status code '%d', got '%d'", http.StatusAccepted, resp.StatusCode)
}

actualSecret, err := kube.CoreV1().Secrets(namespace).Get("testsecret", metav1.GetOptions{})
actualSecret, err := kube.CoreV1().Secrets(namespace).Get(context.TODO(), "testsecret", metav1.GetOptions{})
if err != nil {
t.Errorf("error validting secret: %s", err)
}
Expand Down Expand Up @@ -160,7 +161,7 @@ func Test_SecretsHandler(t *testing.T) {
t.Errorf("want status code '%d', got '%d'", http.StatusAccepted, resp.StatusCode)
}

actualSecret, err := kube.CoreV1().Secrets(namespace).Get("testsecret", metav1.GetOptions{})
actualSecret, err := kube.CoreV1().Secrets(namespace).Get(context.TODO(), "testsecret", metav1.GetOptions{})
if err == nil {
t.Errorf("want not found error, got secret payload '%s'", actualSecret)
}
Expand Down Expand Up @@ -210,7 +211,7 @@ func Test_NamespaceResolver(t *testing.T) {
allowedAnnotation := map[string]string{"openfaas": "true"}
allowedNamespace := "allowed-ns"
ns := &v1.Namespace{ObjectMeta: metav1.ObjectMeta{Name: allowedNamespace, Annotations: allowedAnnotation}}
kube.CoreV1().Namespaces().Create(ns)
kube.CoreV1().Namespaces().Create(context.TODO(), ns, metav1.CreateOptions{})

t.Run("no custom namespace", func(t *testing.T) {
req := httptest.NewRequest("GET", "http://example.com/foo", nil)
Expand Down
9 changes: 5 additions & 4 deletions handlers/update.go
Expand Up @@ -4,6 +4,7 @@
package handlers

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -81,7 +82,7 @@ func updateDeploymentSpec(

deployment, findDeployErr := factory.Client.AppsV1().
Deployments(functionNamespace).
Get(request.Service, getOpts)
Get(context.TODO(), request.Service, getOpts)

if findDeployErr != nil {
return findDeployErr, http.StatusNotFound
Expand Down Expand Up @@ -165,7 +166,7 @@ func updateDeploymentSpec(

if _, updateErr := factory.Client.AppsV1().
Deployments(functionNamespace).
Update(deployment); updateErr != nil {
Update(context.TODO(), deployment, metav1.UpdateOptions{}); updateErr != nil {

return updateErr, http.StatusInternalServerError
}
Expand All @@ -183,7 +184,7 @@ func updateService(

service, findServiceErr := factory.Client.CoreV1().
Services(functionNamespace).
Get(request.Service, getOpts)
Get(context.TODO(), request.Service, getOpts)

if findServiceErr != nil {
return findServiceErr, http.StatusNotFound
Expand All @@ -193,7 +194,7 @@ func updateService(

if _, updateErr := factory.Client.CoreV1().
Services(functionNamespace).
Update(service); updateErr != nil {
Update(context.TODO(), service, metav1.UpdateOptions{}); updateErr != nil {

return updateErr, http.StatusInternalServerError
}
Expand Down
4 changes: 2 additions & 2 deletions k8s/logs.go
Expand Up @@ -107,7 +107,7 @@ func podLogs(ctx context.Context, i v1.PodInterface, pod, container, namespace s
opts.SinceSeconds = parseSince(since)
}

stream, err := i.GetLogs(pod, opts).Stream()
stream, err := i.GetLogs(pod, opts).Stream(context.TODO())
if err != nil {
return err
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func startFunctionPodInformer(ctx context.Context, client kubernetes.Interface,
)

podInformer := factory.Core().V1().Pods()
podsResp, err := client.CoreV1().Pods(namespace).List(metav1.ListOptions{LabelSelector: selector.String()})
podsResp, err := client.CoreV1().Pods(namespace).List(context.TODO(), metav1.ListOptions{LabelSelector: selector.String()})
if err != nil {
log.Printf("PodInformer: %s", err)
return nil, err
Expand Down

0 comments on commit 02266c7

Please sign in to comment.