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

Add createpod element #147

Merged
merged 7 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
golang.org/x/tools v0.0.0-20200812195022-5ae4c3c160a0 // indirect
google.golang.org/grpc v1.35.0
google.golang.org/protobuf v1.25.0
k8s.io/api v0.20.1
k8s.io/apimachinery v0.20.1
k8s.io/client-go v0.20.1
k8s.io/kubelet v0.20.1
Expand Down
77 changes: 77 additions & 0 deletions pkg/networkservice/common/createpod/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Package createpod provides server chain element that creates pods with specified parameters on demand
package createpod

import (
"context"

"github.com/golang/protobuf/ptypes/empty"
"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

const (
nodeNameKey = "NodeNameKey"
)

type createPodServer struct {
client kubernetes.Interface
podTemplate *corev1.Pod
namespace string
}

// NewServer - returns a new server chain element that notifies about long time periods without active requests
d-uzlov marked this conversation as resolved.
Show resolved Hide resolved
func NewServer(client kubernetes.Interface, podTemplate *corev1.Pod, namespace string) networkservice.NetworkServiceServer {
s := &createPodServer{
podTemplate: podTemplate.DeepCopy(),
client: client,
namespace: namespace,
}
return s
}

func (s *createPodServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
nodeName := request.GetConnection().GetLabels()[nodeNameKey]
if nodeName == "" {
return nil, errors.New("NodeNameKey not set")
}

podTemplate := s.podTemplate.DeepCopy()
podTemplate.Spec.NodeName = nodeName
for i := range podTemplate.Spec.Containers {
podTemplate.Spec.Containers[i].Env = append(podTemplate.Spec.Containers[i].Env, corev1.EnvVar{
Name: "NSM_LABELS",
d-uzlov marked this conversation as resolved.
Show resolved Hide resolved
Value: "nodeName: " + nodeName,
})
}

_, err := s.client.CoreV1().Pods(s.namespace).Create(ctx, podTemplate, metav1.CreateOptions{})
d-uzlov marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, errors.WithStack(err)
}

return nil, errors.New("cannot provide required networkservice")
}

func (s *createPodServer) Close(ctx context.Context, conn *networkservice.Connection) (*empty.Empty, error) {
return next.Server(ctx).Close(ctx, conn)
}
97 changes: 97 additions & 0 deletions pkg/networkservice/common/createpod/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
d-uzlov marked this conversation as resolved.
Show resolved Hide resolved
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package createpod_test

import (
"context"
"os"
"testing"
"time"

"github.com/networkservicemesh/api/pkg/api/networkservice"
"github.com/networkservicemesh/sdk/pkg/networkservice/common/clientinfo"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/adapters"
"github.com/networkservicemesh/sdk/pkg/networkservice/core/next"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"

"github.com/networkservicemesh/sdk-k8s/pkg/networkservice/common/createpod"
)

func TestCreatePod(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

clientSet := fake.NewSimpleClientset()

podTemplate := &corev1.Pod{
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "my-container-1",
Image: "my-image-1",
},
{
Name: "my-container-2",
Image: "my-image-2",
},
},
},
}

namespace := "pod-ns-name"

server := next.NewNetworkServiceServer(
adapters.NewClientToServer(clientinfo.NewClient()),
createpod.NewServer(clientSet, podTemplate, namespace),
)

nodeName := "node1"
err := os.Setenv("NODE_NAME", nodeName)
require.NoError(t, err)

_, err = server.Request(ctx, &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{},
})
require.Error(t, err)

podList, err := clientSet.CoreV1().Pods(namespace).List(ctx, metav1.ListOptions{})
require.NoError(t, err)
require.Equal(t, 1, len(podList.Items))
pod := podList.Items[0]

want := corev1.Pod{
Spec: corev1.PodSpec{
NodeName: nodeName,
Containers: []corev1.Container{
{
Name: "my-container-1",
Image: "my-image-1",
Env: []corev1.EnvVar{{Name: "NSM_LABELS", Value: "nodeName: " + nodeName}},
},
{
Name: "my-container-2",
Image: "my-image-2",
Env: []corev1.EnvVar{{Name: "NSM_LABELS", Value: "nodeName: " + nodeName}},
},
},
},
}
require.Equal(t, pod.Spec, want.Spec)
}