github.com/google/cloudprober@v0.11.3/config/runconfig/runconfig.go (about)

     1  // Copyright 2018 The Cloudprober Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  /*
    16  Package runconfig stores cloudprober config that is specific to a single
    17  invocation. e.g., servers injected by external cloudprober users.
    18  */
    19  package runconfig
    20  
    21  import (
    22  	"fmt"
    23  	"sync"
    24  
    25  	rdsserver "github.com/google/cloudprober/rds/server"
    26  	"google.golang.org/grpc"
    27  )
    28  
    29  // runConfig stores cloudprober config that is specific to a single invocation.
    30  // e.g., servers injected by external cloudprober users.
    31  type runConfig struct {
    32  	sync.RWMutex
    33  	grpcSrv   *grpc.Server
    34  	version   string
    35  	rdsServer *rdsserver.Server
    36  }
    37  
    38  var rc runConfig
    39  
    40  // SetDefaultGRPCServer sets the default gRPC server.
    41  func SetDefaultGRPCServer(s *grpc.Server) error {
    42  	rc.Lock()
    43  	defer rc.Unlock()
    44  	if rc.grpcSrv != nil {
    45  		return fmt.Errorf("gRPC server already set to %v", rc.grpcSrv)
    46  	}
    47  	rc.grpcSrv = s
    48  	return nil
    49  }
    50  
    51  // DefaultGRPCServer returns the configured gRPC server and nil if gRPC server
    52  // was not set.
    53  func DefaultGRPCServer() *grpc.Server {
    54  	rc.Lock()
    55  	defer rc.Unlock()
    56  	return rc.grpcSrv
    57  }
    58  
    59  // SetVersion sets the cloudprober version.
    60  func SetVersion(version string) {
    61  	rc.Lock()
    62  	defer rc.Unlock()
    63  	rc.version = version
    64  }
    65  
    66  // Version returns the runconfig version set through the SetVersion() function
    67  // call. It's useful only if called after SetVersion(), otherwise it will
    68  // return an empty string.
    69  func Version() string {
    70  	rc.RLock()
    71  	defer rc.RUnlock()
    72  	return rc.version
    73  }
    74  
    75  // SetLocalRDSServer stores local RDS server in the runconfig. It can later
    76  // be retrieved throuhg LocalRDSServer().
    77  func SetLocalRDSServer(srv *rdsserver.Server) {
    78  	rc.Lock()
    79  	defer rc.Unlock()
    80  	rc.rdsServer = srv
    81  }
    82  
    83  // LocalRDSServer returns the local RDS server, set through the
    84  // SetLocalRDSServer() call.
    85  func LocalRDSServer() *rdsserver.Server {
    86  	rc.RLock()
    87  	defer rc.RUnlock()
    88  	return rc.rdsServer
    89  }