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
11 changed files
with
193 additions
and
55 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,8 +42,6 @@ spec: | |
properties: | ||
address: | ||
type: string | ||
port: | ||
type: string | ||
trustedCA: | ||
type: boolean | ||
type: object | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/klog/v2" | ||
|
||
"github.com/liqotech/liqo/pkg/discovery" | ||
) | ||
|
||
// GetAPIServerAddressFromMasterNode returns the API Server address using the IP of the | ||
// master node of this cluster. The port is always defaulted to 6443. | ||
func GetAPIServerAddressFromMasterNode(ctx context.Context, | ||
clientset kubernetes.Interface) (address string, err error) { | ||
nodes, err := getMasterNodes(ctx, clientset) | ||
if err != nil { | ||
klog.Error(err) | ||
return "", err | ||
} | ||
host, err := discovery.GetAddressFromNodeList(nodes.Items) | ||
if err != nil { | ||
klog.Error(err) | ||
return "", err | ||
} | ||
return fmt.Sprintf("https://%v:6443", host), nil | ||
} | ||
|
||
func getMasterNodes(ctx context.Context, clientset kubernetes.Interface) (*v1.NodeList, error) { | ||
labelSelectors := []string{ | ||
"node-role.kubernetes.io/control-plane", | ||
"node-role.kubernetes.io/master", | ||
} | ||
|
||
nodes := &v1.NodeList{} | ||
var err error | ||
for _, selector := range labelSelectors { | ||
nodes, err = clientset.CoreV1().Nodes().List(ctx, metav1.ListOptions{ | ||
LabelSelector: selector, | ||
}) | ||
if err != nil { | ||
klog.Error(err) | ||
return nodes, err | ||
} | ||
if len(nodes.Items) != 0 { | ||
break | ||
} | ||
} | ||
|
||
if len(nodes.Items) == 0 { | ||
err = fmt.Errorf("no ApiServer.Address variable provided and no master node found, one of the two values must be present") | ||
klog.Error(err) | ||
return nodes, err | ||
} | ||
return nodes, 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,120 @@ | ||
package utils | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/ginkgo/extensions/table" | ||
. "github.com/onsi/gomega" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
|
||
"github.com/liqotech/liqo/pkg/utils/testutil" | ||
) | ||
|
||
func TestAddress(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Address Suite") | ||
} | ||
|
||
var _ = Describe("Address", func() { | ||
|
||
var ( | ||
cluster testutil.Cluster | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
) | ||
|
||
BeforeSuite(func() { | ||
ctx, cancel = context.WithCancel(context.Background()) | ||
|
||
var err error | ||
cluster, _, err = testutil.NewTestCluster([]string{filepath.Join("..", "..", "..", "deployments", "liqo", "crds")}) | ||
if err != nil { | ||
By(err.Error()) | ||
os.Exit(1) | ||
} | ||
}) | ||
|
||
AfterSuite(func() { | ||
cancel() | ||
|
||
err := cluster.GetEnv().Stop() | ||
if err != nil { | ||
By(err.Error()) | ||
os.Exit(1) | ||
} | ||
}) | ||
|
||
type addressTestcase struct { | ||
node *v1.Node | ||
expectedAddress string | ||
} | ||
|
||
DescribeTable("Address table", | ||
|
||
func(c addressTestcase) { | ||
client := kubernetes.NewForConfigOrDie(cluster.GetCfg()) | ||
|
||
node, err := client.CoreV1().Nodes().Create(ctx, c.node, metav1.CreateOptions{}) | ||
Expect(err).To(Succeed()) | ||
node.Status = *c.node.Status.DeepCopy() | ||
node, err = client.CoreV1().Nodes().Update(ctx, node, metav1.UpdateOptions{}) | ||
Expect(err).To(Succeed()) | ||
|
||
address, err := GetAPIServerAddressFromMasterNode(ctx, client) | ||
Expect(err).To(Succeed()) | ||
|
||
Expect(address).To(Equal(c.expectedAddress)) | ||
|
||
Expect(client.CoreV1().Nodes().Delete(ctx, node.Name, metav1.DeleteOptions{})).To(Succeed()) | ||
}, | ||
|
||
Entry("master node", addressTestcase{ | ||
node: &v1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "master-1", | ||
Labels: map[string]string{ | ||
"node-role.kubernetes.io/master": "", | ||
}, | ||
}, | ||
Spec: v1.NodeSpec{}, | ||
Status: v1.NodeStatus{ | ||
Addresses: []v1.NodeAddress{ | ||
{ | ||
Type: v1.NodeExternalIP, | ||
Address: "1.2.3.4", | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedAddress: "https://1.2.3.4:6443", | ||
}), | ||
|
||
Entry("control plane node", addressTestcase{ | ||
node: &v1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "master-1", | ||
Labels: map[string]string{ | ||
"node-role.kubernetes.io/control-plane": "", | ||
}, | ||
}, | ||
Spec: v1.NodeSpec{}, | ||
Status: v1.NodeStatus{ | ||
Addresses: []v1.NodeAddress{ | ||
{ | ||
Type: v1.NodeExternalIP, | ||
Address: "1.2.3.4", | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedAddress: "https://1.2.3.4:6443", | ||
}), | ||
) | ||
|
||
}) |