github.com/deemoprobe/k8s-first-commit@v0.0.0-20230430165612-a541f1982be3/pkg/registry/service_registry.go (about)

     1  /*
     2  Copyright 2014 Google Inc. All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  package registry
    17  
    18  import (
    19  	"encoding/json"
    20  	"net/url"
    21  	"strconv"
    22  	"strings"
    23  
    24  	. "github.com/GoogleCloudPlatform/kubernetes/pkg/api"
    25  	"github.com/GoogleCloudPlatform/kubernetes/pkg/apiserver"
    26  )
    27  
    28  type ServiceRegistry interface {
    29  	ListServices() (ServiceList, error)
    30  	CreateService(svc Service) error
    31  	GetService(name string) (*Service, error)
    32  	DeleteService(name string) error
    33  	UpdateService(svc Service) error
    34  	UpdateEndpoints(e Endpoints) error
    35  }
    36  
    37  type ServiceRegistryStorage struct {
    38  	registry ServiceRegistry
    39  }
    40  
    41  func MakeServiceRegistryStorage(registry ServiceRegistry) apiserver.RESTStorage {
    42  	return &ServiceRegistryStorage{registry: registry}
    43  }
    44  
    45  // GetServiceEnvironmentVariables populates a list of environment variables that are use
    46  // in the container environment to get access to services.
    47  func GetServiceEnvironmentVariables(registry ServiceRegistry, machine string) ([]EnvVar, error) {
    48  	var result []EnvVar
    49  	services, err := registry.ListServices()
    50  	if err != nil {
    51  		return result, err
    52  	}
    53  	for _, service := range services.Items {
    54  		name := strings.ToUpper(service.ID) + "_SERVICE_PORT"
    55  		value := strconv.Itoa(service.Port)
    56  		result = append(result, EnvVar{Name: name, Value: value})
    57  	}
    58  	result = append(result, EnvVar{Name: "SERVICE_HOST", Value: machine})
    59  	return result, nil
    60  }
    61  
    62  func (sr *ServiceRegistryStorage) List(*url.URL) (interface{}, error) {
    63  	return sr.registry.ListServices()
    64  }
    65  
    66  func (sr *ServiceRegistryStorage) Get(id string) (interface{}, error) {
    67  	return sr.registry.GetService(id)
    68  }
    69  
    70  func (sr *ServiceRegistryStorage) Delete(id string) error {
    71  	return sr.registry.DeleteService(id)
    72  }
    73  
    74  func (sr *ServiceRegistryStorage) Extract(body string) (interface{}, error) {
    75  	var svc Service
    76  	err := json.Unmarshal([]byte(body), &svc)
    77  	return svc, err
    78  }
    79  
    80  func (sr *ServiceRegistryStorage) Create(obj interface{}) error {
    81  	return sr.registry.CreateService(obj.(Service))
    82  }
    83  
    84  func (sr *ServiceRegistryStorage) Update(obj interface{}) error {
    85  	return sr.registry.UpdateService(obj.(Service))
    86  }