<commit_before>package dfservice

/*
Copyright 2017-2018 Crunchy Data Solutions, Inc.
Licensed 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.
*/

import (
	"database/sql"
	"errors"
	log "github.com/Sirupsen/logrus"
	crv1 "github.com/crunchydata/postgres-operator/apis/cr/v1"
	"github.com/crunchydata/postgres-operator/apiserver"
	msgs "github.com/crunchydata/postgres-operator/apiservermsgs"
	"github.com/crunchydata/postgres-operator/kubeapi"
	"github.com/crunchydata/postgres-operator/util"
	_ "github.com/lib/pq"
	"k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/resource"
	"k8s.io/client-go/kubernetes"
	"strings"
)

func DfCluster(name, selector string) msgs.DfResponse {
	var err error

	response := msgs.DfResponse{}
	response.Results = make([]msgs.DfDetail, 0)
	response.Status = msgs.Status{Code: msgs.Ok, Msg: ""}

	log.Debugf("selector is %s", selector)
	if selector == "" && name == "all" {
		log.Debug("selector is empty and name is all")
	} else {
		if selector == "" {
			selector = "name=" + name
		}
	}

	//get a list of matching clusters
	clusterList := crv1.PgclusterList{}
	err = kubeapi.GetpgclustersBySelector(apiserver.RESTClient, &clusterList, selector, apiserver.Namespace)

	if err != nil {
		response.Status.Code = msgs.Error
		response.Status.Msg = err.Error()
		return response
	}

	//loop thru each cluster

	log.Debugf("clusters found len is %d\n", len(clusterList.Items))

	for _, c := range clusterList.Items {

		selector := util.LABEL_PG_CLUSTER + "=" + c.Spec.Name

		pods, err := kubeapi.GetPods(apiserver.Clientset, selector, apiserver.Namespace)
		if err != nil {
			response.Status.Code = msgs.Error
			response.Status.Msg = err.Error()
			return response
		}
		for _, p := range pods.Items {
			if strings.Contains(p.Name, "-pgbouncer") ||
				strings.Contains(p.Name, "-pgpool") {
				continue
			}

			var pvcName string
			var found bool
			for _, v := range p.Spec.Volumes {
				if v.Name == "pgdata" {
					found = true
					pvcName = v.VolumeSource.PersistentVolumeClaim.ClaimName

				}
			}
			if !found {
				response.Status.Code = msgs.Error
				response.Status.Msg = "pgdata volume not found in pod "
				return response
			}
			log.Debug("pvc found to be %s", pvcName)<commit_msg>fix pgo show df selector to not include backup jobs<commit_after>package dfservice

/*
Copyright 2017-2018 Crunchy Data Solutions, Inc.
Licensed 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.
*/

import (
	"database/sql"
	"errors"
	log "github.com/Sirupsen/logrus"
	crv1 "github.com/crunchydata/postgres-operator/apis/cr/v1"
	"github.com/crunchydata/postgres-operator/apiserver"
	msgs "github.com/crunchydata/postgres-operator/apiservermsgs"
	"github.com/crunchydata/postgres-operator/kubeapi"
	"github.com/crunchydata/postgres-operator/util"
	_ "github.com/lib/pq"
	"k8s.io/api/core/v1"
	"k8s.io/apimachinery/pkg/api/resource"
	"k8s.io/client-go/kubernetes"
	"strings"
)

func DfCluster(name, selector string) msgs.DfResponse {
	var err error

	response := msgs.DfResponse{}
	response.Results = make([]msgs.DfDetail, 0)
	response.Status = msgs.Status{Code: msgs.Ok, Msg: ""}

	log.Debugf("selector is %s", selector)
	if selector == "" && name == "all" {
		log.Debug("selector is empty and name is all")
	} else {
		if selector == "" {
			selector = "name=" + name
		}
	}

	//get a list of matching clusters
	clusterList := crv1.PgclusterList{}
	err = kubeapi.GetpgclustersBySelector(apiserver.RESTClient, &clusterList, selector, apiserver.Namespace)

	if err != nil {
		response.Status.Code = msgs.Error
		response.Status.Msg = err.Error()
		return response
	}

	//loop thru each cluster

	log.Debugf("clusters found len is %d\n", len(clusterList.Items))

	for _, c := range clusterList.Items {

		selector := util.LABEL_PG_CLUSTER + "=" + c.Spec.Name + "," +
			util.LABEL_PGBACKUP + "!=true"

		pods, err := kubeapi.GetPods(apiserver.Clientset, selector, apiserver.Namespace)
		if err != nil {
			response.Status.Code = msgs.Error
			response.Status.Msg = err.Error()
			return response
		}
		for _, p := range pods.Items {
			if strings.Contains(p.Name, "-pgbouncer") ||
				strings.Contains(p.Name, "-pgpool") {
				continue
			}

			var pvcName string
			var found bool
			for _, v := range p.Spec.Volumes {
				if v.Name == "pgdata" {
					found = true
					pvcName = v.VolumeSource.PersistentVolumeClaim.ClaimName

				}
			}
			if !found {
				response.Status.Code = msgs.Error
				response.Status.Msg = "pgdata volume not found in pod "
				return response
			}
			log.Debug("pvc found to be %s", pvcName)
{'fraction_non_alphanumeric': 0.11920408908360716, 'fraction_numerical': 0.007301935012778387, 'mean_word_length': 3.73961937716263, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2619132', 'n_tokens_mistral': 2043, 'n_tokens_neox': 1854, 'n_words': 490}