istio.io/istio@v0.0.0-20240520182934-d79c90f27776/istioctl/pkg/multixds/google.go (about)

     1  // Copyright Istio 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  package multixds
    16  
    17  import (
    18  	"fmt"
    19  	"net/url"
    20  	"strings"
    21  )
    22  
    23  func isMCPAddr(u *url.URL) bool {
    24  	return strings.HasSuffix(u.Host, ".googleapis.com") || strings.HasSuffix(u.Host, ".googleapis.com:443")
    25  }
    26  
    27  func parseMCPAddr(u *url.URL) (*xdsAddr, error) {
    28  	ret := &xdsAddr{host: u.Host}
    29  	if !strings.HasSuffix(ret.host, ":443") {
    30  		ret.host += ":443"
    31  	}
    32  	const projSeg = "/projects/"
    33  	i := strings.Index(u.Path, projSeg)
    34  	if i == -1 {
    35  		return nil, fmt.Errorf("webhook URL %s doesn't contain the projects segment", u)
    36  	}
    37  	i += len(projSeg)
    38  	j := strings.IndexByte(u.Path[i:], '/')
    39  	if j == -1 {
    40  		return nil, fmt.Errorf("webhook URL %s is malformed", u)
    41  	}
    42  	ret.gcpProject = u.Path[i : i+j]
    43  
    44  	const crSeg = "/ISTIO_META_CLOUDRUN_ADDR/"
    45  	i += j
    46  	j = strings.Index(u.Path[i:], crSeg)
    47  	if j == -1 {
    48  		return nil, fmt.Errorf("webhook URL %s is missing %s", u, crSeg)
    49  	}
    50  	ret.istiod = u.Path[i+j+len(crSeg):]
    51  
    52  	return ret, nil
    53  }