Skip to content

Commit

Permalink
feat: Moved GetFeedGroupNames into hotnews_types and added label to c…
Browse files Browse the repository at this point in the history
…onfig map
  • Loading branch information
werniq committed Sep 6, 2024
1 parent 9b03fc8 commit b877bd0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
32 changes: 32 additions & 0 deletions operator/api/v1/hotnews_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ 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"
)

// HotNewsSpec defines the desired state of HotNews.
Expand Down Expand Up @@ -95,6 +100,33 @@ func init() {
SchemeBuilder.Register(&HotNews{}, &HotNewsList{})
}

// 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
}

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 _, source := range r.Spec.FeedGroups {
if _, exists := configMap.Data[source]; exists {
feedGroups = append(feedGroups, source)
}
}
}

return feedGroups, nil
}

// InitHotNewsStatus func initializes HotNews.Status object with the provided data
func (r *HotNews) InitHotNewsStatus(articlesCount int, requestUrl string, articlesTitles []string) {
r.Status.ArticlesCount = articlesCount
Expand Down
32 changes: 1 addition & 31 deletions operator/api/v1/hotnews_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ import (
"context"
"errors"
"fmt"
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 @@ -138,33 +135,6 @@ func (r *HotNews) ValidateDelete() (admission.Warnings, error) {
return nil, nil
}

// 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
}

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 _, source := range r.Spec.FeedGroups {
if _, exists := configMap.Data[source]; exists {
feedGroups = append(feedGroups, source)
}
}
}

return feedGroups, nil
}

// getAllFeeds returns all feeds in the namespace
// It is used to set the default value for the feeds field in the HotNews resource
func (r *HotNews) getAllFeeds() ([]string, error) {
Expand Down Expand Up @@ -218,7 +188,7 @@ func (r *HotNews) validateHotNews() error {
return nil
}

// feedExists checks if the given list of feeds exist in the namespace
// feedsExists checks if the given list of feeds exist in the namespace
func (r *HotNews) feedsExists() error {
if r.Spec.Feeds == nil {
return nil
Expand Down
2 changes: 2 additions & 0 deletions templates/feed_group_source.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
apiVersion: v1
kind: ConfigMap
metadata:
labels:
feed-group-source: "true"
name: feed-group-source
namespace: operator-system
data:
Expand Down

0 comments on commit b877bd0

Please sign in to comment.