-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
Licensed to the Apache Software Foundation (ASF) under one or more | ||
contributor license agreements. See the NOTICE file distributed with | ||
this work for additional information regarding copyright ownership. | ||
The ASF licenses this file to You 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 kubernetes | ||
|
||
import ( | ||
"context" | ||
"github.com/apache/camel-k/v2/pkg/client" | ||
"github.com/apache/camel-k/v2/pkg/util/sets" | ||
"github.com/apache/camel-k/v2/pkg/util/slice" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// NodeListLookup lookup the list of nodes in the cluster | ||
func NodeListLookup(ctx context.Context, c client.Client) ([]corev1.Node, error) { | ||
nodes, err := c.CoreV1().Nodes().List(ctx, metav1.ListOptions{}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return nodes.Items, err | ||
} | ||
|
||
// NodeArchsLookup lookup the set of node architectures in the cluster | ||
func NodeArchsLookup(ctx context.Context, c client.Client) ([]string, error) { | ||
|
||
nodes, err := NodeListLookup(ctx, c) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
nodeArchs := slice.SliceMap(nodes, func(n corev1.Node) string { | ||
return n.Status.NodeInfo.Architecture | ||
}) | ||
|
||
archSet := sets.NewSet(nodeArchs...) | ||
return archSet.List(), err | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
Licensed to the Apache Software Foundation (ASF) under one or more | ||
contributor license agreements. See the NOTICE file distributed with | ||
this work for additional information regarding copyright ownership. | ||
The ASF licenses this file to You 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 kubernetes | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/apache/camel-k/v2/pkg/client" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestNodeArchLookup(t *testing.T) { | ||
|
||
c, err := client.NewClient(true) | ||
assert.Nil(t, err) | ||
|
||
archs, err := NodeArchLookup(context.Background(), c) | ||
Check failure on line 33 in pkg/util/kubernetes/node_test.go GitHub Actions / builder-it (Jib)
Check failure on line 33 in pkg/util/kubernetes/node_test.go GitHub Actions / common-it-advanced
Check failure on line 33 in pkg/util/kubernetes/node_test.go GitHub Actions / knative-test
Check failure on line 33 in pkg/util/kubernetes/node_test.go GitHub Actions / non-olm
Check failure on line 33 in pkg/util/kubernetes/node_test.go GitHub Actions / builder-it (Spectrum)
Check failure on line 33 in pkg/util/kubernetes/node_test.go GitHub Actions / common-it
Check failure on line 33 in pkg/util/kubernetes/node_test.go GitHub Actions / olm
Check failure on line 33 in pkg/util/kubernetes/node_test.go GitHub Actions / yaks-test
Check failure on line 33 in pkg/util/kubernetes/node_test.go GitHub Actions / upgrade
Check failure on line 33 in pkg/util/kubernetes/node_test.go GitHub Actions / build (ubuntu-latest)
|
||
assert.Nil(t, err) | ||
assert.Equal(t, 1, len(archs)) | ||
|
||
fmt.Printf("Arch: %s\n", archs[0]) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
Licensed to the Apache Software Foundation (ASF) under one or more | ||
contributor license agreements. See the NOTICE file distributed with | ||
this work for additional information regarding copyright ownership. | ||
The ASF licenses this file to You 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 slice | ||
|
||
// SliceFilter retain elements for which the given predicate returns true | ||
func SliceFilter[T any](elements []T, predicate func(T) bool) []T { | ||
var result []T | ||
for _, e := range elements { | ||
if predicate(e) { | ||
result = append(result, e) | ||
} | ||
} | ||
return result | ||
} | ||
|
||
// SliceFold folds elements into a single value determined by the given function | ||
func SliceFold[T any, R any](elements []T, function func(T, R) R) R { | ||
var res R | ||
for _, e := range elements { | ||
res = function(e, res) | ||
} | ||
return res | ||
} | ||
|
||
// SliceMap maps elements to another value according to the given function | ||
func SliceMap[T any, R any](elements []T, function func(T) R) []R { | ||
var mappedValues = make([]R, len(elements)) | ||
for n, e := range elements { | ||
mappedValues[n] = function(e) | ||
} | ||
return mappedValues | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Licensed to the Apache Software Foundation (ASF) under one or more | ||
contributor license agreements. See the NOTICE file distributed with | ||
this work for additional information regarding copyright ownership. | ||
The ASF licenses this file to You 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 slice | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestSliceFilter(t *testing.T) { | ||
|
||
input := []int{1, 2, 3, 4, 5} | ||
was := SliceFilter(input, func(item int) bool { return item%2 == 0 }) | ||
|
||
exp := []int{2, 4} | ||
assert.Equal(t, exp, was) | ||
} | ||
|
||
func TestSliceFold(t *testing.T) { | ||
|
||
input := []int{1, 2, 3, 4, 5} | ||
was := SliceFold(input, func(item, res int) int { return res + item }) | ||
assert.Equal(t, 15, was) | ||
} | ||
|
||
func TestSliceMap(t *testing.T) { | ||
|
||
input := []int{1, 2, 3, 4, 5} | ||
was := SliceMap(input, func(item int) int { return item * 2 }) | ||
|
||
exp := []int{2, 4, 6, 8, 10} | ||
assert.Equal(t, exp, was) | ||
} |