Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/werniq/Go-Gator
Browse files Browse the repository at this point in the history
  • Loading branch information
werniq committed Oct 2, 2024
2 parents d8da5c3 + 9af851c commit ed2c810
Show file tree
Hide file tree
Showing 8 changed files with 698 additions and 191 deletions.
145 changes: 145 additions & 0 deletions CHANGELOG.md

Large diffs are not rendered by default.

33 changes: 8 additions & 25 deletions operator/api/v1/hotnews_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@ limitations under the License.
package v1

import (
"context"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
Expand All @@ -32,15 +28,9 @@ const (
// TypeHotNewsUpdated represents the reason for created condition
TypeHotNewsUpdated = "Updated"

// TypeHotNewsFailedToCreate represents the reason for failed to create condition
TypeHotNewsFailedToCreate = "FailedToCreate"

// HotNewsSuccessfullyCreated represents the reason for created condition
HotNewsSuccessfullyCreated = "HotNews was successfully created"

// HotNewsSuccessfullyUpdated represents the reason for updated condition
HotNewsSuccessfullyUpdated = "HotNews was successfully updated"

// HotNewsError indicates that there were an error during Reconciliation of hot news object
HotNewsError = "Error during processing of hot news creation"

Expand Down Expand Up @@ -105,6 +95,9 @@ type HotNewsStatus struct {
// ArticlesTitles contains a list of titles of first 10 articles
ArticlesTitles []string `json:"articlesTitles"`

// Conditions are used to describe current state of HotNews.
// In case of errors, this field is updated, indicating that error had occurred.
// If Reconciliation was successful - this fields is also updated, showing that everything had gone well.
Conditions map[string]HotNewsConditions `json:"conditions,omitempty"`
}

Expand Down Expand Up @@ -154,30 +147,20 @@ func init() {
}

// GetFeedGroupNames returns all config maps which contain hotNew groups names
func (r *HotNews) GetFeedGroupNames(ctx context.Context) ([]string, error) {
s, err := labels.NewRequirement(FeedGroupLabel, selection.Exists, nil)
if err != nil {
return nil, err
func (r *HotNews) GetFeedGroupNames(configMapList v1.ConfigMapList) []string {
if configMapList.Items == nil {
return nil
}

var configMaps v1.ConfigMapList
err = k8sClient.List(ctx, &configMaps, &client.ListOptions{
LabelSelector: labels.NewSelector().Add(*s),
})
if err != nil {
return nil, err
}

var feedGroups []string
for _, configMap := range configMaps.Items {
for _, configMap := range configMapList.Items {
for _, source := range r.Spec.FeedGroups {
if _, exists := configMap.Data[source]; exists {
feedGroups = append(feedGroups, source)
}
}
}

return feedGroups, nil
return feedGroups
}

// SetCondition initializes status.Conditions if they are empty.
Expand Down
55 changes: 13 additions & 42 deletions operator/api/v1/hotnews_types_test.go
Original file line number Diff line number Diff line change
@@ -1,96 +1,67 @@
package v1

import (
"context"
"errors"
"github.com/stretchr/testify/assert"
v12 "k8s.io/api/core/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
"testing"
)

func TestGetFeedGroupNames(t *testing.T) {
scheme := runtime.NewScheme()
_ = v12.AddToScheme(scheme)

c := fake.NewClientBuilder().WithScheme(scheme).Build()

var res []string
testCases := []struct {
name string
input v12.ConfigMapList
k8sObjects []runtime.Object
expectedGroups []string
expectError bool
setup func()
client client.Client
feedGroups []string
}{
{
name: "No config maps exist",
input: v12.ConfigMapList{},
k8sObjects: []runtime.Object{},
feedGroups: []string{"group1", "group2"},
expectedGroups: res,
client: c,
expectError: false,
},
{
name: "Config map with no matching feed groups",
k8sObjects: []runtime.Object{
&v12.ConfigMap{
ObjectMeta: v1.ObjectMeta{
Name: "config2",
Labels: map[string]string{FeedGroupLabel: "true"},
},
Data: map[string]string{
"othergroup": "some-data",
input: v12.ConfigMapList{
Items: []v12.ConfigMap{
{
ObjectMeta: v1.ObjectMeta{
Name: "config2",
Labels: map[string]string{FeedGroupLabel: "true"},
},
Data: map[string]string{
"othergroup": "some-data",
},
},
},
},
feedGroups: []string{"group1", "group2"},
expectedGroups: res,
client: c,
setup: func() {},
expectError: false,
},
{
name: "Config map listing error",
k8sObjects: nil,
feedGroups: []string{},
setup: func() {
scheme = runtime.NewScheme()
},
expectedGroups: res,
client: fake.NewClientBuilder().WithInterceptorFuncs(
interceptor.Funcs{List: func(ctx context.Context, client client.WithWatch, list client.ObjectList, opts ...client.ListOption) error {
return errors.New("error")
}},
).Build(),
expectError: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
k8sClient = tc.client

hotNews := &HotNews{
Spec: HotNewsSpec{
FeedGroups: tc.feedGroups,
},
}

result, err := hotNews.GetFeedGroupNames(context.TODO())

if tc.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}

result := hotNews.GetFeedGroupNames(tc.input)
assert.Equal(t, tc.expectedGroups, result)
})
}
Expand Down
17 changes: 16 additions & 1 deletion operator/api/v1/hotnews_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ package v1
import (
"context"
"errors"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/selection"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -221,10 +224,22 @@ func (r *HotNews) feedGroupsExists() error {
if r.Spec.FeedGroups == nil {
return nil
}

ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

feedGroups, err := r.GetFeedGroupNames(ctx)
s, err := labels.NewRequirement(FeedGroupLabel, selection.Exists, nil)
if err != nil {
return err
}

var configMaps v1.ConfigMapList
err = k8sClient.List(ctx, &configMaps, &client.ListOptions{
LabelSelector: labels.NewSelector().Add(*s),
Namespace: r.Namespace,
})

feedGroups := r.GetFeedGroupNames(configMaps)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit ed2c810

Please sign in to comment.