mirror of https://github.com/knative/pkg.git
Update knative/pkg/test (#50)
This commit is contained in:
parent
3ca427071d
commit
8f6a3be149
|
@ -19,7 +19,11 @@ limitations under the License.
|
||||||
package test
|
package test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/knative/pkg/test/logging"
|
||||||
|
"github.com/knative/pkg/test/spoof"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
|
k8styped "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||||
"k8s.io/client-go/rest"
|
"k8s.io/client-go/rest"
|
||||||
"k8s.io/client-go/tools/clientcmd"
|
"k8s.io/client-go/tools/clientcmd"
|
||||||
)
|
)
|
||||||
|
@ -29,6 +33,11 @@ type KubeClient struct {
|
||||||
Kube *kubernetes.Clientset
|
Kube *kubernetes.Clientset
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewSpoofingClient returns a spoofing client to make requests
|
||||||
|
func NewSpoofingClient(client *KubeClient, logger *logging.BaseLogger, domain string, resolvable bool) (*spoof.SpoofingClient, error) {
|
||||||
|
return spoof.New(client.Kube, logger, domain, resolvable)
|
||||||
|
}
|
||||||
|
|
||||||
// NewKubeClient instantiates and returns several clientsets required for making request to the
|
// NewKubeClient instantiates and returns several clientsets required for making request to the
|
||||||
// kube client specified by the combination of clusterName and configPath. Clients can make requests within namespace.
|
// kube client specified by the combination of clusterName and configPath. Clients can make requests within namespace.
|
||||||
func NewKubeClient(configPath string, clusterName string) (*KubeClient, error) {
|
func NewKubeClient(configPath string, clusterName string) (*KubeClient, error) {
|
||||||
|
@ -56,3 +65,22 @@ func buildClientConfig(kubeConfigPath string, clusterName string) (*rest.Config,
|
||||||
&overrides).ClientConfig()
|
&overrides).ClientConfig()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateConfigMap updates the config map for specified @name with values
|
||||||
|
func (client *KubeClient) UpdateConfigMap(name string, configName string, values map[string]string) error {
|
||||||
|
configMap, err := client.getConfigMap(name).Get(configName, metav1.GetOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for key, value := range values {
|
||||||
|
configMap.Data[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = client.getConfigMap(name).Update(configMap)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// getConfigMap gets the knative serving config map.
|
||||||
|
func (client *KubeClient) getConfigMap(name string) k8styped.ConfigMapInterface {
|
||||||
|
return client.Kube.CoreV1().ConfigMaps(name)
|
||||||
|
}
|
||||||
|
|
|
@ -24,10 +24,9 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/knative/pkg/test/logging"
|
||||||
"github.com/knative/pkg/test/spoof"
|
"github.com/knative/pkg/test/spoof"
|
||||||
"go.opencensus.io/trace"
|
"go.opencensus.io/trace"
|
||||||
"go.uber.org/zap"
|
|
||||||
"k8s.io/client-go/kubernetes"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// MatchesAny is a NOP matcher. This is useful for polling until a 200 is returned.
|
// MatchesAny is a NOP matcher. This is useful for polling until a 200 is returned.
|
||||||
|
@ -81,12 +80,12 @@ func EventuallyMatchesBody(expected string) spoof.ResponseChecker {
|
||||||
// the domain in the request headers, otherwise it will make the request directly to domain.
|
// the domain in the request headers, otherwise it will make the request directly to domain.
|
||||||
// desc will be used to name the metric that is emitted to track how long it took for the
|
// desc will be used to name the metric that is emitted to track how long it took for the
|
||||||
// domain to get into the state checked by inState. Commas in `desc` must be escaped.
|
// domain to get into the state checked by inState. Commas in `desc` must be escaped.
|
||||||
func WaitForEndpointState(kubeClientset *kubernetes.Clientset, logger *zap.SugaredLogger, domain string, inState spoof.ResponseChecker, desc string, resolvable bool) error {
|
func WaitForEndpointState(kubeClient *KubeClient, logger *logging.BaseLogger, domain string, inState spoof.ResponseChecker, desc string, resolvable bool) error {
|
||||||
metricName := fmt.Sprintf("WaitForEndpointState/%s", desc)
|
metricName := fmt.Sprintf("WaitForEndpointState/%s", desc)
|
||||||
_, span := trace.StartSpan(context.Background(), metricName)
|
_, span := trace.StartSpan(context.Background(), metricName)
|
||||||
defer span.End()
|
defer span.End()
|
||||||
|
|
||||||
client, err := spoof.New(kubeClientset, logger, domain, resolvable)
|
client, err := NewSpoofingClient(kubeClient, logger, domain, resolvable)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.uber.org/zap"
|
"github.com/knative/pkg/test/logging"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
"k8s.io/client-go/kubernetes"
|
"k8s.io/client-go/kubernetes"
|
||||||
|
@ -46,6 +46,7 @@ type Response struct {
|
||||||
Body []byte
|
Body []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Interface defines the actions that can be performed by the spoofing client.
|
||||||
type Interface interface {
|
type Interface interface {
|
||||||
Do(*http.Request) (*Response, error)
|
Do(*http.Request) (*Response, error)
|
||||||
Poll(*http.Request, ResponseChecker) (*Response, error)
|
Poll(*http.Request, ResponseChecker) (*Response, error)
|
||||||
|
@ -71,7 +72,7 @@ type SpoofingClient struct {
|
||||||
endpoint string
|
endpoint string
|
||||||
domain string
|
domain string
|
||||||
|
|
||||||
logger *zap.SugaredLogger
|
logger *logging.BaseLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a SpoofingClient that rewrites requests if the target domain is not `resolveable`.
|
// New returns a SpoofingClient that rewrites requests if the target domain is not `resolveable`.
|
||||||
|
@ -79,7 +80,7 @@ type SpoofingClient struct {
|
||||||
// follow the ingress if it moves (or if there are multiple ingresses).
|
// follow the ingress if it moves (or if there are multiple ingresses).
|
||||||
//
|
//
|
||||||
// If that's a problem, see test/request.go#WaitForEndpointState for oneshot spoofing.
|
// If that's a problem, see test/request.go#WaitForEndpointState for oneshot spoofing.
|
||||||
func New(kubeClientset *kubernetes.Clientset, logger *zap.SugaredLogger, domain string, resolvable bool) (*SpoofingClient, error) {
|
func New(kubeClientset *kubernetes.Clientset, logger *logging.BaseLogger, domain string, resolvable bool) (*SpoofingClient, error) {
|
||||||
sc := SpoofingClient{
|
sc := SpoofingClient{
|
||||||
Client: http.DefaultClient,
|
Client: http.DefaultClient,
|
||||||
RequestInterval: requestInterval,
|
RequestInterval: requestInterval,
|
||||||
|
|
Loading…
Reference in New Issue