This repository has been archived by the owner on Feb 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
305 additions
and
79 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
Copyright 2019 Gravitational, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License 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 ops | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/gravitational/gravity/lib/defaults" | ||
"github.com/gravitational/gravity/lib/storage" | ||
"github.com/gravitational/gravity/lib/utils" | ||
|
||
"github.com/gravitational/trace" | ||
) | ||
|
||
// ClusterEndpoints contains system cluster endpoints such as Teleport | ||
// proxy address or cluster control panel URL. | ||
type ClusterEndpoints struct { | ||
// Internal contains internal cluster endpoints. | ||
Internal clusterEndpoints | ||
// Public contains public cluster endpoints. | ||
Public clusterEndpoints | ||
} | ||
|
||
// AuthGateways returns all auth gateway endpoints. | ||
func (e ClusterEndpoints) AuthGateways() []string { | ||
if len(e.Public.AuthGateways) > 0 { | ||
return e.Public.AuthGateways | ||
} | ||
return e.Internal.AuthGateways | ||
} | ||
|
||
// FirstAuthGateway returns the first auth gateway endpoint. | ||
func (e ClusterEndpoints) FirstAuthGateway() string { | ||
gateways := e.AuthGateways() | ||
if len(gateways) > 0 { | ||
return gateways[0] | ||
} | ||
return "" | ||
} | ||
|
||
// ManagementURLs returns all cluster management URLs. | ||
func (e ClusterEndpoints) ManagementURLs() []string { | ||
if len(e.Public.ManagementURLs) > 0 { | ||
return e.Public.ManagementURLs | ||
} | ||
return e.Internal.ManagementURLs | ||
} | ||
|
||
// clusterEndpoints combines various types of cluster endpoints. | ||
type clusterEndpoints struct { | ||
// AuthGateways is a list of Teleport proxy addresses. | ||
AuthGateways []string | ||
// ManagementURLs is a list of URLs pointing to cluster dashboard. | ||
ManagementURLs []string | ||
} | ||
|
||
// GetClusterEndpoints returns system endpoints for the specified cluster. | ||
func GetClusterEndpoints(operator Operator, key SiteKey) (*ClusterEndpoints, error) { | ||
cluster, err := operator.GetSite(key) | ||
if err != nil { | ||
return nil, trace.Wrap(err) | ||
} | ||
gateway, err := operator.GetAuthGateway(key) | ||
if err != nil { | ||
return nil, trace.Wrap(err) | ||
} | ||
return getClusterEndpoints(cluster, gateway) | ||
} | ||
|
||
func getClusterEndpoints(cluster *Site, gateway storage.AuthGateway) (*ClusterEndpoints, error) { | ||
// Internal endpoints point directly to master nodes. | ||
var internal clusterEndpoints | ||
for _, master := range cluster.Masters() { | ||
internal.AuthGateways = append(internal.AuthGateways, | ||
utils.EnsurePort(master.AdvertiseIP, strconv.Itoa(defaults.GravitySiteNodePort))) | ||
internal.ManagementURLs = append(internal.ManagementURLs, | ||
utils.EnsurePortURL(master.AdvertiseIP, strconv.Itoa(defaults.GravitySiteNodePort))) | ||
} | ||
// Public endpoints are configured via auth gateway resource. | ||
var public clusterEndpoints | ||
for _, address := range gateway.GetWebPublicAddrs() { | ||
public.AuthGateways = append(public.AuthGateways, | ||
utils.EnsurePort(address, defaults.HTTPSPort)) | ||
public.ManagementURLs = append(public.ManagementURLs, | ||
utils.EnsurePortURL(address, defaults.HTTPSPort)) | ||
} | ||
return &ClusterEndpoints{ | ||
Internal: internal, | ||
Public: public, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
Copyright 2019 Gravitational, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License 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 ops | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/gravitational/gravity/lib/defaults" | ||
"github.com/gravitational/gravity/lib/schema" | ||
"github.com/gravitational/gravity/lib/storage" | ||
"github.com/gravitational/gravity/lib/utils" | ||
|
||
"gopkg.in/check.v1" | ||
) | ||
|
||
type EndpointsSuite struct{} | ||
|
||
var _ = check.Suite(&EndpointsSuite{}) | ||
|
||
func (s *EndpointsSuite) TestClusterEndpoints(c *check.C) { | ||
master1 := storage.Server{ | ||
AdvertiseIP: "192.168.1.1", | ||
ClusterRole: string(schema.ServiceRoleMaster), | ||
} | ||
node := storage.Server{ | ||
AdvertiseIP: "192.168.1.2", | ||
ClusterRole: string(schema.ServiceRoleNode), | ||
} | ||
master2 := storage.Server{ | ||
AdvertiseIP: "192.168.1.3", | ||
ClusterRole: string(schema.ServiceRoleMaster), | ||
} | ||
cluster := &Site{ | ||
ClusterState: storage.ClusterState{ | ||
Servers: []storage.Server{ | ||
master1, node, master2, | ||
}, | ||
}, | ||
} | ||
gateway := storage.DefaultAuthGateway() | ||
|
||
endpoints, err := getClusterEndpoints(cluster, gateway) | ||
c.Assert(err, check.IsNil) | ||
c.Assert(endpoints, check.DeepEquals, &ClusterEndpoints{ | ||
Internal: clusterEndpoints{ | ||
AuthGateways: []string{ | ||
utils.EnsurePort(master1.AdvertiseIP, strconv.Itoa(defaults.GravitySiteNodePort)), | ||
utils.EnsurePort(master2.AdvertiseIP, strconv.Itoa(defaults.GravitySiteNodePort)), | ||
}, | ||
ManagementURLs: []string{ | ||
utils.EnsurePortURL(master1.AdvertiseIP, strconv.Itoa(defaults.GravitySiteNodePort)), | ||
utils.EnsurePortURL(master2.AdvertiseIP, strconv.Itoa(defaults.GravitySiteNodePort)), | ||
}, | ||
}, | ||
Public: clusterEndpoints{}, | ||
}) | ||
c.Assert(endpoints.AuthGateways(), check.DeepEquals, []string{ | ||
utils.EnsurePort(master1.AdvertiseIP, strconv.Itoa(defaults.GravitySiteNodePort)), | ||
utils.EnsurePort(master2.AdvertiseIP, strconv.Itoa(defaults.GravitySiteNodePort)), | ||
}) | ||
c.Assert(endpoints.FirstAuthGateway(), check.Equals, | ||
utils.EnsurePort(master1.AdvertiseIP, strconv.Itoa(defaults.GravitySiteNodePort))) | ||
c.Assert(endpoints.ManagementURLs(), check.DeepEquals, []string{ | ||
utils.EnsurePortURL(master1.AdvertiseIP, strconv.Itoa(defaults.GravitySiteNodePort)), | ||
utils.EnsurePortURL(master2.AdvertiseIP, strconv.Itoa(defaults.GravitySiteNodePort)), | ||
}) | ||
|
||
publicAddr := "cluster.example.com:444" | ||
gateway.SetPublicAddrs([]string{publicAddr}) | ||
|
||
endpoints, err = getClusterEndpoints(cluster, gateway) | ||
c.Assert(err, check.IsNil) | ||
c.Assert(endpoints, check.DeepEquals, &ClusterEndpoints{ | ||
Internal: clusterEndpoints{ | ||
AuthGateways: []string{ | ||
utils.EnsurePort(master1.AdvertiseIP, strconv.Itoa(defaults.GravitySiteNodePort)), | ||
utils.EnsurePort(master2.AdvertiseIP, strconv.Itoa(defaults.GravitySiteNodePort)), | ||
}, | ||
ManagementURLs: []string{ | ||
utils.EnsurePortURL(master1.AdvertiseIP, strconv.Itoa(defaults.GravitySiteNodePort)), | ||
utils.EnsurePortURL(master2.AdvertiseIP, strconv.Itoa(defaults.GravitySiteNodePort)), | ||
}, | ||
}, | ||
Public: clusterEndpoints{ | ||
AuthGateways: []string{ | ||
publicAddr, | ||
}, | ||
ManagementURLs: []string{ | ||
fmt.Sprintf("https://%v", publicAddr), | ||
}, | ||
}, | ||
}) | ||
c.Assert(endpoints.AuthGateways(), check.DeepEquals, []string{ | ||
publicAddr, | ||
}) | ||
c.Assert(endpoints.FirstAuthGateway(), check.Equals, publicAddr) | ||
c.Assert(endpoints.ManagementURLs(), check.DeepEquals, []string{ | ||
fmt.Sprintf("https://%v", publicAddr), | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.