xds_handshake_cluster.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /*
  2. * Copyright 2021 gRPC authors.
  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 internal
  17. import (
  18. "google.golang.org/grpc/attributes"
  19. "google.golang.org/grpc/resolver"
  20. )
  21. // handshakeClusterNameKey is the type used as the key to store cluster name in
  22. // the Attributes field of resolver.Address.
  23. type handshakeClusterNameKey struct{}
  24. // SetXDSHandshakeClusterName returns a copy of addr in which the Attributes field
  25. // is updated with the cluster name.
  26. func SetXDSHandshakeClusterName(addr resolver.Address, clusterName string) resolver.Address {
  27. addr.Attributes = addr.Attributes.WithValue(handshakeClusterNameKey{}, clusterName)
  28. return addr
  29. }
  30. // GetXDSHandshakeClusterName returns cluster name stored in attr.
  31. func GetXDSHandshakeClusterName(attr *attributes.Attributes) (string, bool) {
  32. v := attr.Value(handshakeClusterNameKey{})
  33. name, ok := v.(string)
  34. return name, ok
  35. }