// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. // // Licensed under the Apache License, Version 2.0 (the "License"). You may // not use this file except in compliance with the License. A copy of the // License is located at // // http://aws.amazon.com/apache2.0/ // // or in the "license" file accompanying this file. This file 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 service import ( "context" "github.com/aws/amazon-vpc-resource-controller-k8s/test/framework/utils" v1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/wait" "sigs.k8s.io/controller-runtime/pkg/client" ) type Manager interface { GetService(ctx context.Context, namespace string, name string) (*v1.Service, error) CreateService(ctx context.Context, service *v1.Service) (*v1.Service, error) DeleteService(ctx context.Context, service *v1.Service) error } type defaultManager struct { k8sClient client.Client } func NewManager(k8sClient client.Client) Manager { return &defaultManager{k8sClient: k8sClient} } func (s *defaultManager) GetService(ctx context.Context, namespace string, name string) (*v1.Service, error) { service := &v1.Service{} err := s.k8sClient.Get(ctx, types.NamespacedName{ Namespace: namespace, Name: name, }, service) return service, err } func (s *defaultManager) CreateService(ctx context.Context, service *v1.Service) (*v1.Service, error) { err := s.k8sClient.Create(ctx, service) if err != nil { return nil, err } observedService := &v1.Service{} return observedService, wait.PollUntil(utils.PollIntervalShort, func() (bool, error) { if err := s.k8sClient.Get(ctx, utils.NamespacedName(service), observedService); err != nil { return false, err } return true, nil }, ctx.Done()) } func (s *defaultManager) DeleteService(ctx context.Context, service *v1.Service) error { err := s.k8sClient.Delete(ctx, service) if err != nil { return err } return nil }