# Sock Shop Groups
#
# Copyright 2024 by Cloudsoft Corporation Limited

name: Sock Shop Groups
id: sock-shop-groups
description: |
  This discovers the Kubernetes resources in the sock-shop namespace and creates two
  management groups with specific subsets of the discovered resources: Shopping Cart
  resources and User resources.
services:
  - type: kubernetes-discovery
    id: discovered-resources
    name: Discovered Resources
    location: kubernetes-sockshop
    brooklyn.config:
      namespace: sock-shop
      kubernetes.auto-organize-labels: false

  - type: org.apache.brooklyn.entity.stock.BasicApplication
    name: Resource Groups
    brooklyn.children:
      - type: org.apache.brooklyn.entity.group.DynamicGroup
        name: Shopping Cart Pods
        brooklyn.config:
          dynamicgroup.entityfilter:
            $brooklyn:object:
              type: com.google.common.base.Predicates
              factoryMethod.name: "and"
              factoryMethod.args:
                - $brooklyn:object:
                    type: kubernetes-discovery-predicate-is-pod
                - $brooklyn:object:
                    type: kubernetes-discovery-predicate-has-label
                    constructor.args:
                      - "app.kubernetes.io/component"
                      - "Carts"
        brooklyn.policies:
          - type: org.apache.brooklyn.policy.InvokeEffectorOnSensorChange
            brooklyn.config:
              sensor: kubernetes.resource.hash.list
              effector: rescanEntities
        brooklyn.enrichers:
          - type: org.apache.brooklyn.enricher.stock.Propagator
            brooklyn.config:
              enricher.producer: $brooklyn:entity("discovered-resources")
              enricher.propagating.inclusions:
                - kubernetes.resource.hash.list

      - type: org.apache.brooklyn.entity.group.DynamicGroup
        name: User Pods
        brooklyn.config:
          dynamicgroup.entityfilter:
            $brooklyn:object:
              type: com.google.common.base.Predicates
              factoryMethod.name: "and"
              factoryMethod.args:
                - $brooklyn:object:
                    type: kubernetes-discovery-predicate-is-pod
                - $brooklyn:object:
                    type: kubernetes-discovery-predicate-name-matches
                    constructor.args:
                      - "^user.*"
        brooklyn.policies:
          - type: org.apache.brooklyn.policy.InvokeEffectorOnSensorChange
            brooklyn.config:
              sensor: kubernetes.resource.hash.list
              effector: rescanEntities
        brooklyn.enrichers:
          - type: org.apache.brooklyn.enricher.stock.Propagator
            brooklyn.config:
              enricher.producer: $brooklyn:entity("discovered-resources")
              enricher.propagating.inclusions:
                - kubernetes.resource.hash.list

